Playing sound effects
As mentioned in the previous section, when playing sound effects, we need to handle the pausing and resuming of the sound effects if they are still playing when the app is put in the background, for instance, to check something else on your phone, as this is not currently handled by the library.
We will initially update our Character
class, which is our top-level base class for all our sprites, to add onPaused
and onResumed
callbacks, which all our sprites can use.
We will then listen for life cycle change events in our game and if these are called, we will iterate over all our sprites and pass on these events.
And finally, as the sound effects are related to George, we will update the George
class to play sounds and pause and resume these sound effects when needed. Let's get started:
- Open up the
character.dart
file. At the bottom of theCharacter
class, add the following function definitions:void onPaused() {} void onResumed() {}