当前位置:网站首页>Unity publishes a method of webgl playing sound
Unity publishes a method of webgl playing sound
2022-07-02 22:34:00 【ttod】
The way to start playing sound is to mp3 Documents or ogg Files in StreamingAssets Under the folder , Load it locally before playing , But released as WebGL after , There are many problems in the process of playing sound , It is an error to check the relevant code about sound playback through the web console , I can't understand the specific content , Finally, I took the method of using the web page itself to play sound . The specific method is to first add audio Elements , Then the audio playback is realized by controlling this element .
The sample code of the page is as follows :
<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>
In the code above , Added... To the page 4 individual audio Elements , And the element id It uses the same name as the audio to be played . meanwhile , It is said that most browsers do not allow automatic audio playback ( At least in the Chrome Inside is not allowed ), So in body Click... Is written in the element body Code for playing audio :
οnclick="if(!isPlayBird){PlayAudio('Bird',true);isPlayBird = 'true';}
Here, in order to work only with one click , Don't work in the future , Added a isPlayBird Variables to limit .
Let's talk about unity Call... On the page PlayAudio Method approach .
First of all, it's in Asset Add under folder Plugins Folder , Add a folder with the extension jslib The file of , The contents of the document are as follows :
mergeInto(LibraryManager.library, {
PlayHtmlAudio: function (audioName,isPlay) {
PlayAudio(UTF8ToString(audioName),isPlay);
}
});
The code uses UTF8ToString Method replaces the original Pointer_stringify Method to handle strings . In fact, both methods are possible , But use Pointer_stringify Method words , The browser will alarm , Say this method is out of date , It is recommended to use UTF8ToString Replace , Sure enough , Use UTF8ToString No more alarms after the method .
The following is the C# Call in script PlayHtmlAudio Method reference code :
[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);
}
}
Okay , That's it .
边栏推荐
- Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- An overview of the development of affective computing and understanding research
- Market Research - current market situation and future development trend of third-party data platform
- Struct, bit segment, enumeration, union
- #include errors detected. Please update your includePath.
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- 【外刊】睡眠与减肥
- 【leetcode】1380. Lucky number in matrix
猜你喜欢
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
Daily book -- analyze the pain points of software automation from simple to deep
Simpleitk use - 3 Common operations
Sql service intercepts string
Etcd raft protocol
How to write a good program when a big book speaks every day?
Phpcms realizes the direct Alipay payment function of orders
[001] [arm-cortex-m3/4] internal register
Landingsite eband B1 smoke test case
What is it that makes you tremble? Those without fans can learn
随机推荐
Reading experience of just because
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
【leetcode】1380. Lucky number in matrix
kubernetes资源对象介绍及常用命令(四)
Web侧防御指南
Market Research - current market situation and future development trend of genome editing mutation detection kit
About test cases
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
Pointer - function pointer
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Utilisation de simpletk - 4. Question étrange
Basic concepts of image and deep understanding of yuv/rgb
:last-child 不生效解决
【C 题集】of Ⅴ
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Market Research - current situation and future development trend of preclinical medical device testing service market
Servicemesh mainly solves three pain points
Destroy in beforedestroy invalid value in localstorage
APP页面分享口令Rails实现
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?