Playing background music
In this section, we will add the music playback, which will load at the start of the game and continue playing while we play the game. The Flame audio library has a static class called Bgm
(background music) that adds music to the audio cache and will pause and resume the music when the app is backgrounded or brought back to the front. We get this functionality built into the Bgm
class, so it requires very little code to get the music loaded and playing. As you will see in the next section regarding the playing of sound effects, we must do a bit more work to ensure sound effects pause and resume when backgrounded. Unfortunately, this is how it currently works in the Flame library at this time, but hopefully, this will be improved in future versions of the library.
To add the music playback to the code, do the following:
- Open the
main.dart
file and import theFlame
audio library:import 'package:flame_audio/flame_audio.dart';
- At the top...