当前位置:网站首页>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);
}
}好了,就到这里吧。
边栏推荐
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- [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)
- A specially designed loss is used to deal with data sets with unbalanced categories
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Daily book CSO advanced road first exposed
- Micro service gateway selection, please accept my knees!
- Official announcement! The golden decade of new programmers and developers was officially released
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- Ransack组合条件搜索实现
- 将 EMQX Cloud 数据通过公网桥接到 AWS IoT
猜你喜欢

Ransack组合条件搜索实现
![[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)](/img/07/6f2dfb543cb0ab4f27169da7e6ad07.jpg)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)

VictoriaMetrics 简介

New feature of go1.18: introduce new netip Network Library

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

From "bronze" to "King", there are three secrets of enterprise digitalization

Promise optimized callback hell

TinyMCE visual editor adds Baidu map plug-in

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

Oriental Aesthetics and software design
随机推荐
Web侧防御指南
【剑指 Offer】56 - I. 数组中数字出现的次数
Market Research - current situation and future development trend of cell-based seafood market
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
Une semaine de vie
一周生活
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Unity3D学习笔记4——创建Mesh高级接口
记录一下微信、QQ、微博分享web网页功能
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
SQL必需掌握的100个重要知识点:管理事务处理
TinyMCE visual editor adds Baidu map plug-in
Introduction to the principle of geographical detector
Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
VictoriaMetrics 简介
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
How do I access the kubernetes API?
Image segmentation using pixellib
[zero foundation I] Navicat download link