我在做一个 HTML5 游戏。我使用createjs来预加载图像。图像预加载完成后,需要自动播放背景音乐。
我知道 chrome 的新音频政策。我很困惑我使用函数handleComplete来触发音频。为什么它仍然不起作用?我应该如何使它工作?
这是我的代码:
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();
}
我发现的错误是:
NotAllowedError: play() failed because the user didn't interact with the document first.
慕莱坞2482426
RISEBY
相关分类