Disable game audio depending on device support (Phaser 3)

I’ve seen a few games that freeze in Safari/iOS when OGG-only sounds are used.

this.load.audio('woof', 'woof.ogg');
// …
this.sound.add('woof');
// Error: There is no audio asset with key "woof" in the audio cache

You should probably disable audio on these devices so the game is still playable.

Phaser

new Phaser.Game({
  audio: {},
  callbacks: {
    preBoot: function(game) {
      var ogg = game.device.audio.ogg;

      if (!ogg) {
        game.events.off('prestep', game.sound.update, game.sound);
        game.events.off('destroy', game.sound.destroy, game.sound);
        game.sound.destroy();
        game.config.audio.noAudio = true;
        game.sound = Phaser.Sound.SoundManagerCreator.create(game);
      }
    }
  }
});

No Phaser

var Game = require('phaser/src/core/Game');
var deviceAudio = require('phaser/src/device/Audio');
// …

new Game({
  audio: { noAudio: !deviceAudio.ogg }
});
1 Like

@samme : Excellent catch on this bug! :slight_smile: I will definitely use this. Now I’d like to know how to get the code to stop playing one sound & start playing a chosen sound.