当前位置:网站首页>Unity发布WebGL播放声音的一种方法
Unity发布WebGL播放声音的一种方法
2022-07-02 22:01:00 【ttod】
一开始播放声音的方法是将mp3文件或者ogg文件放到StreamingAssets文件夹下,先加载到本地再播放,但是发布成WebGL之后,在播放声音的过程中问题不断,通过网页控制台查看关于声音播放的的相关代码是报错的,具体内容也看不太懂,最后还是走了利用网页自身播放声音的方法。具体做法就是首先在网页上添加audio元素,然后通过控制该元素来实现音频播放。
页面示例代码如下:
<body style="text-align: center" onclick="if(!isPlayBird){PlayAudio('Bird',true);isPlayBird = 'true';}">
<audio id="Bird" preload="auto" src="StreamingAssets/Audios/Bird.mp3" type="audio/mpeg" loop="true"></audio>
<audio id="Rain" preload="auto" src="StreamingAssets/Audios/Rain.mp3" type="audio/mpeg" loop="true"></audio>
<audio id="Wind" preload="auto" src="StreamingAssets/Audios/Wind.mp3" type="audio/mpeg" loop="true"></audio>
<audio id="XiShuai" preload="auto" src="StreamingAssets/Audios/XiShuai.mp3" type="audio/mpeg" loop="true"></audio>
<script>
var isPlayBird = false;
function PlayAudio(audioName,isplay) {
var player = document.getElementById(audioName);
if(isplay)
{
player.play();
}
else
{
player.pause();
}
}
</script>
</body>在上面的代码中,页面上添加了4个audio元素,并且元素的id和需要播放的音频使用了相同的名字。同时,由于据说大部分浏览器不允许自动播放音频(至少在Chrome里是不不允许的),于是在body元素里面写了点击body播放音频的代码:
οnclick="if(!isPlayBird){PlayAudio('Bird',true);isPlayBird = 'true';}
这里为了只在点击一下起作用,以后就不要起作用了,加了一个isPlayBird变量来做限制。
下面说一下在unity中调用页面上的PlayAudio方法的做法。
首先是要在Asset文件夹下添加Plugins文件夹,在该文件夹中添加一个扩展名为jslib的文件,文件内容参考如下:
mergeInto(LibraryManager.library, {
PlayHtmlAudio: function (audioName,isPlay) {
PlayAudio(UTF8ToString(audioName),isPlay);
}
});代码中使用UTF8ToString方法替代了原来的Pointer_stringify方法来处理字符串。其实两种方法都是可以的,但是使用Pointer_stringify方法的话,浏览器会报警告,说这个方法已经过时,建议使用UTF8ToString替换,果然,使用UTF8ToString方法之后不再报警告了。
下面是在C#脚本中调用PlayHtmlAudio方法的参考代码:
[DllImport("__Internal")]
private static extern void PlayHtmlAudio(string audioName, bool isPlay);
public void PlayAudio(string audioName, bool isPlay)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayHtmlAudio(audioName, isPlay);
}
}好了,就到这里吧。
边栏推荐
- Share how to make professional hand drawn electronic maps
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
- Market Research - current market situation and future development trend of handheld wound imaging equipment
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Hanoi Tower problem
- 加了定位的文字如何水平垂直居中
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- Ransack combined condition search implementation
- Gee: (II) resampling the image
- Attack and defense world PWN question: Echo
猜你喜欢

Evolution of messaging and streaming systems under the native tide of open source cloud

关于测试用例

The difference between include < > and include ""

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source

Off chip ADC commissioning record

How to write a good program when a big book speaks every day?

sql service 截取字符串

An overview of the development of affective computing and understanding research

基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习

Etcd Raft 协议
随机推荐
Landingsite eband B1 smoke test case
Pointer and string
Sql service intercepts string
An overview of the development of affective computing and understanding research
Market Research - current situation and future development trend of preclinical medical device testing service market
ServiceMesh主要解决的三大痛点
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
Etcd Raft 协议
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Chargement de l'image pyqt après décodage et codage de l'image
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
*C language final course design * -- address book management system (complete project + source code + detailed notes)
[shutter] shutter resource file use (import resource pictures | use image resources)
Servicemesh mainly solves three pain points
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
How do I access the kubernetes API?
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
kubernetes资源对象介绍及常用命令(四)
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
【leetcode】1380. Lucky number in matrix