Phaser 声音仅在 Chrome 上停止工作 Android

Phaser sound stops working only on Chrome for Android

前几分钟声音正常,但过了一会儿 .play() 不播放任何声音。似乎如果有很长一段时间没有声音播放,声音就会停止工作。

它在桌面、iOS 和通用 android 浏览器上运行良好。我只是 运行 遇到这个问题,特别是在 android 设备上使用移动 chrome 作为浏览器。

你有什么版本的 Phaser 和 android?对我来说,我尝试播放 4 分钟的歌曲似乎没有问题。即使屏幕消失,恢复时也可以毫无问题地继续。一个肮脏的解决方案是添加一个循环,每 2 分钟播放一次无声声音,例如 "refresh" 声音管理器,如果这样可以解决您的问题。

事实证明这是一个 Chrome 错误,如果 30 秒左右没有声音播放,该错误会导致网络音频停止播放声音。

https://code.google.com/p/chromium/issues/detail?id=518863

修复似乎是观看

audioContext.currentTime

当它在 30 秒后卡住时创建一个新的 audioContext。

我最终使用的解决方案如下:

请注意,我正在使用移相器库 - 所以这个确切的解决方案不适合你 - 但它会给你一个大概的想法

//This is run using a timer event every second

//this.game.time.events.loop(1000, this.checkAudioContext, this);

evil.AudioManager.prototype.checkAudioContext=function(){

    //work out when the audio context has stopped

    if(this.game.sound.context.currentTime-this.last_context_time===0){

        //close out the existing context and create a new one
        //you will also need new gain nodes if you are using them

        this.game.sound.context.close();  
        this.game.sound.context=new AudioContext(); 
        this.game.sound.masterGain= this.game.sound.context.createGain(); 
        this.game.sound.masterGain.gain.volume=this.volume;
        this.game.sound.masterGain.connect(this.game.sound.context.destination);

        //now go through every sound and connect them to the new context
        //creating gain nodes as we go.
        for(var key in this.tracks){

            var snd=this.tracks[key].snd;
            snd.context=this.game.sound.context;
            snd.masterGainNode = this.game.sound.masterGain;
            snd.gainNode=this.game.sound.context.createGain();          
            snd.gainNode.gain.value = snd.volume * this.volume;
            snd.gainNode.connect(snd.masterGainNode);
        }       

    }else{
    //update out time variable
    this.last_context_time=ctx.currentTime;
    }
}