当前位置:网站首页>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);
}
}好了,就到这里吧。
边栏推荐
- An overview of the development of affective computing and understanding research
- 图像基础概念与YUV/RGB深入理解
- Record the functions of sharing web pages on wechat, QQ and Weibo
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- [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)
- 攻防世界pwn题:Recho
- Market Research - current situation and future development trend of carob chocolate market
- 【剑指 Offer】57. 和为s的两个数字
- Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
- 一周生活
猜你喜欢

Ransack combined condition search implementation

Kubernetes resource object introduction and common commands (4)

Ransack组合条件搜索实现

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)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[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)

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
![[staff] Sibelius 7.5.1 score software installation (software download | software installation)](/img/1a/4932a7931c54248c065cf8a1462d34.jpg)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)

Basic concepts of image and deep understanding of yuv/rgb

An overview of the development of affective computing and understanding research

Introduction to the principle of geographical detector
随机推荐
The difference between include < > and include ""
sql service 截取字符串
分享一下如何制作专业的手绘电子地图
Market Research - current market situation and future development trend of genome editing mutation detection kit
如何访问kubernetes API?
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
tinymce可视化编辑器增加百度地图插件
Learn computer knowledge from scratch
GEE:(二)对影像进行重采样
【C 题集】of Ⅴ
Basic concepts of image and deep understanding of yuv/rgb
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
Image segmentation using pixellib
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
技术人创业:失败不是成功,但反思是
Daily book -- analyze the pain points of software automation from simple to deep
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
The failure rate is as high as 80%. What should we do about digital transformation?
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
Technical solution of vision and manipulator calibration system