我在做一個(gè) HTML5 游戲。我使用createjs來(lái)預(yù)加載圖像。圖像預(yù)加載完成后,需要自動(dòng)播放背景音樂(lè)。我知道 chrome 的新音頻政策。我很困惑我使用函數(shù)handleComplete來(lái)觸發(fā)音頻。為什么它仍然不起作用?我應(yīng)該如何使它工作?這是我的代碼:function Sound (id_str){ this.id = id_str; }Sound.prototype.play = function () { //there are other logics, but can simply like below let audio = document.getElementById(this.id); audio.play().catch(e =>console.log(e));};var audio_bg = new Sound('bgm_id');windows.onload = function preload (handleFileProgress,handleComplete){ let manifest = [...]; loader = new createjs.LoadQueue(false); loader.setMaxConnections(100); loader.maintainScriptOrder=true; loader.installPlugin(createjs.Sound); loader.loadManifest(manifest); loader.addEventListener('progress', handleFileProgress); loader.addEventListener('complete', handleComplete);};function handleFileProgress(){...}function handleComplete(){ //... audio_bg.play();}我發(fā)現(xiàn)的錯(cuò)誤是:NotAllowedError: play() failed because the user didn't interact with the document first.
使用createjs系列的preload.js調(diào)用函數(shù)后音頻沒(méi)有播放
慕田峪4524236
2021-09-17 21:02:34