欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決Babylon.js中AudioContext was not allowed to start異常問題

 更新時(shí)間:2023年04月03日 14:52:13   作者:方寸匠心  
這篇文章主要介紹了解決Babylon.js中AudioContext was not allowed to start異常問題方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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)文章

  • JavaScript判斷是否手機(jī)瀏覽器的五種方法

    JavaScript判斷是否手機(jī)瀏覽器的五種方法

    現(xiàn)在手機(jī)網(wǎng)站已經(jīng)很普及了,有時(shí)候前端網(wǎng)頁需要判斷,用戶使用的是手機(jī)瀏覽器還是電腦瀏覽器。這篇文章整理了JavaScript判斷是否手機(jī)瀏覽器的五種方法,通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2022-11-11
  • JavaScript中的Promise詳解

    JavaScript中的Promise詳解

    這篇文章主要介紹了JavaScript中的Promise使用詳解,promise對(duì)象是JS進(jìn)階學(xué)習(xí)中的重要知識(shí)點(diǎn),需要的朋友可以參考下
    2021-10-10
  • js實(shí)現(xiàn)卡片式項(xiàng)目管理界面UI設(shè)計(jì)效果

    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-12
  • JavaScript如何獲取到導(dǎo)航條中HTTP信息

    JavaScript如何獲取到導(dǎo)航條中HTTP信息

    這篇文章主要為大家詳細(xì)介紹了JavaScript如何獲取到導(dǎo)航條中HTTP信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • javascript 面向?qū)ο罄^承

    javascript 面向?qū)ο罄^承

    javascript 面向?qū)ο罄^承,需要的朋友可以參考下。
    2009-11-11
  • JS字符串拼接的幾種方式(最新推薦)

    JS字符串拼接的幾種方式(最新推薦)

    在 JavaScript 中,使用字符串連接有 幾 種方式:連接符(+)、反引號(hào)(`)、join()、concat(),這篇文章主要介紹了JS字符串拼接的幾種方式,需要的朋友可以參考下
    2023-01-01
  • JavaScript加強(qiáng)之自定義event事件

    JavaScript加強(qiáng)之自定義event事件

    event事件在本文以自定義的方式出現(xiàn),感興趣的朋友可以參考下,希望對(duì)大家有所幫助
    2013-09-09
  • js和jquery中獲取非行間樣式

    js和jquery中獲取非行間樣式

    本篇文章主要介紹了js和jquery中獲取非行間樣式的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-05-05
  • JS 數(shù)組 移除 實(shí)現(xiàn)代碼

    JS 數(shù)組 移除 實(shí)現(xiàn)代碼

    可以移除數(shù)組中的值函數(shù) ,用來取出數(shù)組中的一些值,間接的修改了數(shù)組值。
    2009-07-07
  • 微信小程序調(diào)用PHP后臺(tái)接口 解析純html文本

    微信小程序調(diào)用PHP后臺(tái)接口 解析純html文本

    這篇文章主要為大家詳細(xì)介紹了微信小程序調(diào)用PHP后臺(tái)接口,解析純html文本的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論