AudioContext was not allowed to start

I am having a (seemingly) common issue with getting sound to work on iOS. I’ve tried paring everything down to a very base-bones version, and I’m still not able to get it working there. I don’t own a Mac either, so no access to the iPad console.

I am literally using the same code as in the example (http://labs.phaser.io/edit.html?src=src\audio\Web%20Audio\play%20audio%20file.js), and yet - it does not work?

I get multiples of the “AudioContext was not allowed to start” warning in Chrome:
image
Though eventually it plays (on Desktop that is). Though the example I copied does not get these warnings, so I suspect the source of my problem might be whatever causes the warnings.

I have tried “forcing” the AudioContext to resume on pointerup/pointerdown, but the state remains suspended.

game.js:

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    pixelArt: true,
    scene: {
        preload: preload,
        create: create,
		update: update
    }
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.audio('theme', [
        'assets/audio/oedipus_wizball_highscore.ogg',
        'assets/audio/oedipus_wizball_highscore.mp3'
    ]);

}
var debugTxt;
function create ()
{
    var music = this.sound.add('theme');

    music.play();
	debugTxt = this.add.text(400,40,"");
}
function update (timestep, dt)
{
    debugTxt.setText("sound state is: " + game.sound.context.state);
}

Answering my own question, this did the trick for me:

			this.input.addDownCallback(function() {
				
				if (game.sound.context.state === 'suspended') {
					game.sound.context.resume();
				}
				
			});

Before y’all think I didn’t bother to read the documentation or even the warning given, I had that exact code bound to “pointerdown” and/or “pointerup” - but apparently that doesn’t cut it. Crossposting the same answer as on the other thread as this one may be slightly more, er, googleable?

It’s “normal” to get one AudioContext was not allowed to start warning, I think, because Phaser tries to create a Web Audio context automatically when the game boots. Can you expand the warnings in the console and look at the stack trace?

I think Phaser also tries to unlock (resume) the context automatically when a user clicks.

Phaser queues these events, so Chrome doesn’t consider them as coming after a user gesture, I think.

1 Like

Thanks for the reply!

Yeah, I mean it is just a warning, and the AudioContext does indeed not start at the time, so getting the warning is entirely correct. After doing the fix outlined above, I only get the initial warning, and not multiples.

Yeah, and neither does iOS. Chrome would eventually start the AudioContext when clicking around, but Safari steadfastly refused to - until I hooked it up via addDownCallback, not sure why that’s different from pointerup/down, but apparently it is.

Hi,
where did you put this code exactly?
thanks

In the “create” function.

Thank you for your suggest.
I’ve added in create function but unfortunately the problem persists.


Mail priva di virus. www.avast.com

I also tried putting it in my create and it didn’t work for me too. Weird thing is it works on my old computer which runs on a earlier version of chrome.

Hi , im having the same issue have you figured how to solve it?

hi , were you able to solve it?

Hey there, I solved it by adding a timeout after the first user interaction, like so:

window.setTimeout(() => {
      this.backgroundMusic.play();
}, 500);

This fixed it for me.

Hi adding this part of code to the update function, will solve the problem

if (game.sound.context.state === 'suspended') {
					game.sound.context.resume();
}

You don’t need to resume the audio context yourself, Phaser will do that automatically.