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 }
});
2 Likes

@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.

Since Phaser v3.23 these lines are unnecessary.

I’m interested in changing Phaser to allow this:

// Hypothetical code
new Phaser.Game({
  callbacks: {
    preBoot: function (game) {
      game.config.audio.noAudio = !game.device.audio.ogg;
    }
  }
});

It would require instantiating the Sound Manager later, during game boot.