BrettJephson
BrettJephson

Reputation: 416

Webaudio sound stops on Chrome for Android after about 2 minutes

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

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

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.

Upvotes: 1

Views: 3383

Answers (3)

ARMistice
ARMistice

Reputation: 118

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.

Upvotes: 7

BrettJephson
BrettJephson

Reputation: 416

@RaymondToy's comment 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.

Upvotes: 0

cwilso
cwilso

Reputation: 13918

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.

Upvotes: 0

Related Questions