大约2分钟后,Chrome for Android会停止播放Webaudio声音

Bre*_*son 1 javascript android google-chrome web-audio-api

我在Chrome for Android上遇到了WebAudio问题.

我在三星Galaxy S3(GT-I9300)上遇到这种情况:

  • Chrome版本44.0.2403.133
  • Android 4.3.0

这是我用来尝试隔离问题的代码:

var audioContext;
if(window.AudioContext) {
    audioContext = new AudioContext();
}

var startTime = Date.now();
var lastTrigger;

var gain = audioContext.createGain();
gain.gain.value = 1;
gain.connect(audioContext.destination);

var buttonTrigger = document.getElementById('trigger');
buttonTrigger.addEventListener('click', function(event) {
    var oscillator =  audioContext.createOscillator();
    oscillator.type = "square";
    oscillator.frequency.value = 100 +  (Math.cos(audioContext.currentTime)*100);
    oscillator.connect(gain);
    oscillator.start(0);
    oscillator.stop(audioContext.currentTime+0.1);

    lastTrigger = Date.now();
});

var timer = document.getElementById('timer');
setInterval(function() {
    if(lastTrigger) { timer.textContent = Date.now() - lastTrigger; }
}, 1000);
Run Code Online (Sandbox Code Playgroud)

在这里它是在jsfiddle

这只是创建一个振荡器节点并在单击按钮时播放.在我的手机上,如果我没有点击按钮大约一分半钟或两分钟,我就不再发出任何声音了.

没有抛出任何错误.

有没有人有这个问题的经验和可能的解决方法?

这个问题最初出现在一个更大的应用程序中,使用Phaser播放来自m4a文件的声音,所以这不仅仅与振荡器有关.

UPDATE

根据Chromium bug票证,此问题现已修复.

ARM*_*ice 7

在Android上遇到同样的问题之后.我找到了比每30秒播放一次"虚拟声音"更好的解决方案.

记住你上次玩过你上下文的时间:

var lastPlayed = new Date().getTime();
var audioSource = context.createBufferSource();
    audioSource.connect( context.destination );
    audioSource.buffer = sb;
    audioSource.start( 0 );
Run Code Online (Sandbox Code Playgroud)

下次播放样本/声音时只需检查传递的时间并重置AudioContext

if(new Date().getTime()-lastPlayed>30000){   // Time passed since last playing is greater than 30 secs
     context.close();
     context=new AudioContext();
}
Run Code Online (Sandbox Code Playgroud)

对于Android,这就像魅力一样.