当前位置:网站首页>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);
}
}
好了,就到这里吧。
边栏推荐
- 地理探测器原理介绍
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- Market Research - current market situation and future development trend of total nutrition products
- How to write a good program when a big book speaks every day?
- [shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
- Attack and defense world PWN question: Echo
- Unity3d learning notes 4 - create mesh advanced interface
- Daily book - low code you must understand in the era of digital transformation
- #include<>和#include“”的区别
- Market Research - current market situation and future development trend of genome editing mutation detection kit
猜你喜欢
[shutter] shutter layout component (wrap component | expanded component)
[shutter] shutter resource file use (import resource pictures | use image resources)
Redis distributed lock failure, I can't help but want to burst
Etcd Raft 协议
C语言,实现三子棋小游戏
A specially designed loss is used to deal with data sets with unbalanced categories
kubernetes资源对象介绍及常用命令(四)
地理探测器原理介绍
[001] [arm-cortex-m3/4] internal register
[shutter] shutter gesture interaction (small ball following the movement of fingers)
随机推荐
LightGBM原理及天文数据中的应用
Web side defense Guide
技术人创业:失败不是成功,但反思是
Promise optimized callback hell
540. Single element in ordered array
Daily book CSO advanced road first exposed
20220702-程序员如何构建知识体系?
[shutter] shutter layout component (wrap component | expanded component)
Market Research - current situation and future development trend of sickle cell therapy Market
A week's life
About test cases
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
服务可见可观测性
SQL必需掌握的100个重要知识点:使用游标
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
LandingSite eBand B1冒烟测试用例
Market Research - current market situation and future development trend of third-party data platform
Service visibility and observability
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
pyqt圖片解碼 編碼後加載圖片