unlockMobileAudio static method

void unlockMobileAudio()

A helper method to unlock audio on mobile devices.

Some mobile devices (like iOS) do not allow audio playback by default. Call this method in the first onTouchBegin event to unlock the website for audio playback.

stage.onTouchBegin.first.then((e) {
  SoundMixer.unlockMobileAudio();
});

Implementation

static void unlockMobileAudio() {
  if (engine == SoundEngine.WebAudioApi) {
    try {
      final context = WebAudioApiMixer.audioContext;
      final source = context.createBufferSource();
      source.buffer = context.createBuffer(1, 1, 22050);
      source.connectNode(context.destination!);
      source.start(0);
    } catch (e) {
      // There is nothing we can do :(
    }
  }
}