当前位置:网站首页>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 .
边栏推荐
- 《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Reading experience of just because
- PHP微信抢红包的算法
- Web侧防御指南
- U++ 原始内存 学习笔记
- 数据库系统概论第一章简答题-期末考得怎么样?
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- 《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
猜你喜欢
SimpleITK使用——4. 奇怪的問題
20220702-程序员如何构建知识体系?
Socket socket c/s end process
Kubernetes resource object introduction and common commands (4)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
情感计算与理解研究发展概述
Daily book -- analyze the pain points of software automation from simple to deep
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Official announcement! The golden decade of new programmers and developers was officially released
C language, to achieve three chess games
随机推荐
Market Research - current situation and future development trend of marine clutch Market
[leetcode] sword finger offer 11 Rotate the minimum number of the array
About test cases
Market Research - current market situation and future development trend of genome editing mutation detection kit
20220702 how do programmers build knowledge systems?
Unity3D学习笔记4——创建Mesh高级接口
Kubernetes resource object introduction and common commands (4)
Market Research - current situation and future development trend of anti-counterfeiting label market
Simpleitk use - 4 Strange question
Unity3d learning notes 4 - create mesh advanced interface
Lightgbm principle and its application in astronomical data
[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)
Struct, bit segment, enumeration, union
Ransack combined condition search implementation
540. Single element in ordered array
Socket socket c/s end process
UE4 游戏架构 学习笔记
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Market Research - current situation and future development trend of herringbone gear Market
Reading experience of just because