Resume a suspended Web Audio context (v2.6.2)

Re. Chrome AutoPlay Policy Changes.

If you create your AudioContext on page load, you’ll have to call resume() at some time after the user interacted with the page (e.g., user clicked a button). Alternatively, the AudioContext will be resumed after a user gesture if start() is called on any attached node.

Resume the context from a user gesture

Phaser CE does this automatically, so this is for v2.6.2 only.

var snd = this.sound;

if (snd.usingWebAudio && snd.context.state !== 'running') {
  this.input.onDown.addOnce(function () {
    snd.context.resume();
  });
}

Play a sound from a user gesture

The browser resumes the context automatically.

var audio = this.add.audio('KEY');
      
this.input.onDown.addOnce(function () {
  audio.play();
});

I’m surprise onDown works for these, but — it seems to.

4 Likes

In Phaser III, this also applies when using “html” or “web audio”