解決Babylon.js中AudioContext was not allowed to start異常問題
BabylonJS中使用音頻引擎時(shí)遇到錯(cuò)誤
當(dāng)在BabylonJS中使用音頻引擎時(shí),可能會(huì)遇到以下錯(cuò)誤:
audioEngine.ts:172 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
這個(gè)錯(cuò)誤是因?yàn)闉g覽器的安全策略要求音頻上下文必須在用戶事件(例如單擊、鍵盤按鍵等)中啟用。這意味著,如果您嘗試在沒有用戶事件的情況下自動(dòng)播放音樂,則會(huì)拋出此錯(cuò)誤。
為了解決這個(gè)問題,我們需要啟用音頻上下文。在BabylonJS中,可以通過AudioEngine()
類來創(chuàng)建音頻上下文。
您可以使用以下代碼在用戶事件觸發(fā)后啟用音頻上下文:
let audioContext = new AudioEngine().audioContext; audioContext.resume();
在以上示例中,我們創(chuàng)建了一個(gè)AudioEngine
實(shí)例,并從中獲取了音頻上下文。接下來,我們調(diào)用resume()
方法啟用音頻上下文。請(qǐng)注意,在啟用之前,必須先檢查音頻上下文是否存在。如果不存在,則需要將其創(chuàng)建。
這樣就可以成功地啟用音頻上下文,然后您可以在BabylonJS中播放音頻文件而不會(huì)遇到錯(cuò)誤。
示例源碼(修改前):
playingSoundSprites = (scene: Scene, canvas: HTMLCanvasElement) => { var freeCamera = new FreeCamera("freeCamera", new Vector3(0, 0, 0), scene); var theSound = new Sound("allSounds", "https://playground.babylonjs.com/sounds/6sounds.mp3", scene, null, {autoplay: false}); var isPlaying = 0; let audioContext = new AudioEngine().audioContext; var soundArray = [ [0.0, 5.000], [5.100, 6.600], [12.000, 1.600], [14.000, 9.200], [23.000, 7.900], [31.000, 2.800], ]; theSound.onended = function() { isPlaying = 0; console.log("not playing"); }; var advanceTexture = AdvancedDynamicTexture.CreateFullscreenUI("UI"); var uiPanel = new StackPanel(); uiPanel.width = "220px"; uiPanel.fontSize = "14px"; uiPanel.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER; uiPanel.verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER; advanceTexture.addControl(uiPanel); var button = Button.CreateSimpleButton("but", "Play All Sounds"); button.paddingTop = "10px"; button.width = "150px"; button.height = "50px"; button.color = "white"; button.background = "green"; button.onPointerDownObservable.add(function() { if (isPlaying === 0) { isPlaying = 1; theSound.play(); console.log("playing"); } }); uiPanel.addControl(button); var button1 = Button.CreateSimpleButton("but1", "Play Random Sound"); button1.paddingTop = "10px"; button1.width = "150px"; button1.height = "50px"; button1.color = "white"; button1.background = "green"; button1.onPointerDownObservable.add(function() { if (isPlaying === 0) { isPlaying = 1; var randomSound = Math.floor(Math.random() * 6); theSound.play(0, soundArray[randomSound][0], soundArray[randomSound][1]); console.log("playing"); } }); uiPanel.addControl(button1); return scene; }
運(yùn)行結(jié)果如下:
不能播放聲音,在瀏覽器控制臺(tái)
查看日志如下:
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
示例源碼(修改后):
playingSoundSprites = (scene: Scene, canvas: HTMLCanvasElement) => { var freeCamera = new FreeCamera("freeCamera", new Vector3(0, 0, 0), scene); var theSound = new Sound("allSounds", "https://playground.babylonjs.com/sounds/6sounds.mp3", scene, null, {autoplay: false}); var isPlaying = 0; var soundArray = [ [0.0, 5.000], [5.100, 6.600], [12.000, 1.600], [14.000, 9.200], [23.000, 7.900], [31.000, 2.800], ]; theSound.onended = function() { isPlaying = 0; console.log("not playing"); }; var advanceTexture = AdvancedDynamicTexture.CreateFullscreenUI("UI"); var uiPanel = new StackPanel(); uiPanel.width = "220px"; uiPanel.fontSize = "14px"; uiPanel.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER; uiPanel.verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER; advanceTexture.addControl(uiPanel); var button = Button.CreateSimpleButton("but", "Play All Sounds"); button.paddingTop = "10px"; button.width = "150px"; button.height = "50px"; button.color = "white"; button.background = "green"; button.onPointerDownObservable.add(function() { let audioContext = new AudioEngine().audioContext; audioContext!.resume().then(() => { if (isPlaying === 0) { isPlaying = 1; theSound.play(); console.log("playing"); } }); }); uiPanel.addControl(button); var button1 = Button.CreateSimpleButton("but1", "Play Random Sound"); button1.paddingTop = "10px"; button1.width = "150px"; button1.height = "50px"; button1.color = "white"; button1.background = "green"; button1.onPointerDownObservable.add(function() { let audioContext = new AudioEngine().audioContext; audioContext!.resume().then(() => { if (isPlaying === 0) { isPlaying = 1; var randomSound = Math.floor(Math.random() * 6); theSound.play(0, soundArray[randomSound][0], soundArray[randomSound][1]); console.log("playing"); } }); }); uiPanel.addControl(button1); return scene; }
以上就是解決Babylon.js中AudioContext was not allowed to start異常問題的詳細(xì)內(nèi)容,更多關(guān)于解決Babylon.js異常的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js實(shí)現(xiàn)卡片式項(xiàng)目管理界面UI設(shè)計(jì)效果
這篇文章主要介紹了js實(shí)現(xiàn)卡片式項(xiàng)目管理界面UI設(shè)計(jì)效果,該UI設(shè)計(jì)中,將各個(gè)項(xiàng)目以卡片的方式堆疊排列在屏幕上,當(dāng)點(diǎn)擊了其中的某個(gè)項(xiàng)目的時(shí)候,該項(xiàng)目圖片會(huì)全屏放大,向下滾動(dòng)鼠標(biāo)可以看到該項(xiàng)目的介紹信息,需要的朋友可以參考下2015-12-12JavaScript如何獲取到導(dǎo)航條中HTTP信息
這篇文章主要為大家詳細(xì)介紹了JavaScript如何獲取到導(dǎo)航條中HTTP信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10JavaScript加強(qiáng)之自定義event事件
event事件在本文以自定義的方式出現(xiàn),感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-09-09微信小程序調(diào)用PHP后臺(tái)接口 解析純html文本
這篇文章主要為大家詳細(xì)介紹了微信小程序調(diào)用PHP后臺(tái)接口,解析純html文本的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06