最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webaudio sound stops on Chrome for Android after about 2 minutes - Stack Overflow

programmeradmin1浏览0评论

I'm running into an issue with WebAudio on Chrome for Android.

I'm experiencing this on a Samsung Galaxy S3 (GT-I9300) with:

  • Chrome version 44.0.2403.133
  • Android 4.3.0

Here is the code I'm using to try and isolate the issue:

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);

And here it is on jsfiddle

This simply creates an oscillator node and plays on clicking a button. On my phone, if I do not click the button for about a minute and a half or two minutes, I no longer get any sound.

There are no errors thrown.

Does anyone have any experience of this issue and a possible workaround?

This issue originally appeared in a much larger app using Phaser to play sounds from a m4a file, so this is not solely to do with the oscillator.

UPDATE

According to the Chromium bug ticket this issue has now been fixed.

I'm running into an issue with WebAudio on Chrome for Android.

I'm experiencing this on a Samsung Galaxy S3 (GT-I9300) with:

  • Chrome version 44.0.2403.133
  • Android 4.3.0

Here is the code I'm using to try and isolate the issue:

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);

And here it is on jsfiddle

This simply creates an oscillator node and plays on clicking a button. On my phone, if I do not click the button for about a minute and a half or two minutes, I no longer get any sound.

There are no errors thrown.

Does anyone have any experience of this issue and a possible workaround?

This issue originally appeared in a much larger app using Phaser to play sounds from a m4a file, so this is not solely to do with the oscillator.

UPDATE

According to the Chromium bug ticket this issue has now been fixed.

Share Improve this question edited Feb 17, 2016 at 19:51 BrettJephson asked Aug 26, 2015 at 20:30 BrettJephsonBrettJephson 4165 silver badges13 bronze badges 2
  • 1 As an aside, gain.value = 1 should be gain.gain.value = 1. It doesn't make a difference in this example because the default gain is 1, but it would matter if you wanted to change the gain. See the GainNode documentation from MDN for more details. – DJG Commented Nov 21, 2015 at 17:12
  • Thanks for pointing that out. I've edited the original post. – BrettJephson Commented Nov 22, 2015 at 18:25
Add a ment  | 

3 Answers 3

Reset to default 7

After experiencing the same problem on Android. I found a better solution than playing a "dummy sound" every 30sec.

Just remember the time when you last played over your context:

var lastPlayed = new Date().getTime();
var audioSource = context.createBufferSource();
    audioSource.connect( context.destination );
    audioSource.buffer = sb;
    audioSource.start( 0 );

The next time you play a sample/sound Just check the time passed and reset the AudioContext

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

For Android this works like charm.

I think what you're seeing is an auto-shutdown of Web Audio when there's no sound for a while. What happens if you click the button a second time, a second or so after the first? (Web Audio can take some time (order of tens of milliseconds, at least) to restart.)

The suspend()/resume() methods, and looking at the context.state, would be helpful here.

@RaymondToy's ment answers this question. There is a bug with Chrome on Android (at least for Samsung Galaxy S3/4). Webaudio stops playing sounds after a period of inactivity. Where inactivity is essentially silence.

The only work around I can find is to play some kind of sound at intervals. I have experimented with playing a sound every 30 seconds and that stopped the problem.

I also tried playing some kind of silent noise (silent audio buffer or silent part of an m4a audio file or muted sound), neither of which solved the problem.

发布评论

评论列表(0)

  1. 暂无评论