I have been battling an iOS 13 issue that affects all WebAudio in Phaser in both Safari and Cordova-build iOS apps. Basically, whenever you plug in headphones, then unplug them, sound disappears from your phaser game. This is not a phaser-specific issue: it affects all HTML5 web audio calls on iOS 13. You can replicate it with your own game, or any of the Phaser examples, by starting the game, playing audio, plugging in headphones into the headphone jacks, and unplugging them.
So, I am thinking that the audio context is caught in some sort of bad state, and needs to be completely replaced in order to fix the problem. But, when I try to replace the audio context with the new setAudioContext(), I get errors. Can anyone give me some guidance on replacing the audio context in a running phaser game?
Here’s the error in the console:
Error: DOMException: Failed to execute ‘connect’ on ‘AudioNode’: cannot connect to a destination belonging to a different audio context.
at createBufferSource (/{mypath}/node_modules/phaser/dist/phaser.min.js:1:1)
at createAndStartBufferSource (/{mypath}/node_modules/phaser/dist/phaser.min.js:1:1)
at play (/{mypath}node_modules/phaser/dist/phaser.min.js:1:1)
I have confirmed that recreating the audio context helps phaser regain sound when it is lost in these case:
headphones plugged or unplugged; or
the virtual keyboard is deactivated during a sound playing.
I had to put a setting dialog in my game to allow the user to reconstruct the audio context. When they press a button, the game uses this code to activate the new context:
var newContext = new ((window).AudioContext || (window).webkitAudioContext)();
this.sound.setAudioContext(newContext);
NOTE: if you have any audiosprites constructed before this operation, you must reconstruct them after setting the new audio context.