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 get multiples of the “AudioContext was not allowed to start” warning in Chrome:
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.
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.
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.