flutterflamebackground-musicflame-audioflame-game

Flutter flame's flame_audio background music is not working on web platform


I am trying to play background music for my Flutter Flame Game, but the music is not playing on Web platform. Testing on android device, the same code works fine and plays music.

Checking with docs, there seems to be no issue or separate code for the Web platform. Also, the pub.dev shows web as a platform for the flame_audio.

enter image description here

Note: Other sound effects in web are working fine with the flame_audio package

I am using:

  flame: ^1.8.2
  flame_tiled: ^1.13.0
  flame_audio: ^2.1.1

Code below is written in the onLoad() of the Class extending FlameGame:

  @override
  FutureOr<void> onLoad() async {
  ..... // other code
  FlameAudio.bgm.initialize();
  FlameAudio.bgm.play('bg_music.ogg');

  return super.onLoad();
  }

Some YouTube videos suggest to play music on key press event, if is it so, why is it not documented?


Solution

  • Web browsers have the restriction that you have to have interacted with the page before they can play any audio, try for example adding a button that the user has to click before starting the background music and it should work.

    You should currently get an error in your browser console giving you an error message about this too.