Skip to content

Latest commit

 

History

History
96 lines (62 loc) · 2.86 KB

bgm.md

File metadata and controls

96 lines (62 loc) · 2.86 KB

Looping Background Music

The Bgm class with the management of loooping background music tracks with regards to application (or game) lifecycle state changes.

When the application is paused, terminated, or sent to background, Bgm will automatically pause the currently playing music track. Similarly, when the application is resumed, Bgm will resume the background music. Manual pause and resume is also supported.

For this class to function properly, the observer must be registered by calling the following:

Flame.bgm.initialize();

IMPORTANT Note: The initialize function must be called at a point in time where an instance of the WidgetsBinding class already exists. It's safe to assume that this case is true at any point after runApp has been called at least once.

In cases where you're done with background music but still want to keep the application/game running, use the dispose function to remove the observer.

Flame.bgm.dispose();

To play a looping background music track, run:

import 'package:flame/flame.dart';

Flame.bgm.play('adventure-track.mp3');

Or, if you prefer:

import 'package:flame/bgm.dart';

Bgm audio = Bgm();
audio.play('adventure-track.mp3');

Note: The Bgm class will always use the static instance of FlameAudio in Flame.audio for storing cached music files.

You must have an appropriate folder structure and add the files to the pubspec.yaml file, as explained in Flame Audio documentation.

Caching music files

The following functions can be used to preload (and unload) music files into the cache. These functions are just aliases for the ones in Flame.audio with the same names.

Again, please refer to the Flame Audio documentation if you need more info.

Flame.audio.load('adventure-track.mp3');
Flame.audio.loadAll([
  'menu.mp3',
  'dungeon.mp3',
]);
Flame.audio.clear('adventure-track.mp3');
Flame.bgm.clearAll();

Methods

Play

The play function takes in a String that should be a filename that points to the location of the music file to be played (following the Flame Audio folder structure requirements).

You can pass an additional optional double parameter, the volume (defaults to 1.0).

Examples:

Flame.bgm.play('bgm/boss-fight/level-382.mp3');
Flame.bgm.play('bgm/world-map.mp3', volume: .25);

Stop

To stop a currently playing background music track, just call stop.

Flame.bgm.stop();

Pause and Resume

To manually pause and resume background music you can use the pause and resume functions.

Flame.bgm automatically handles pausing and resuming the currently playing background music track. Manually pausing the function prevents the app/game from auto-resuming when focus is given back to the app/game.

Flame.bgm.pause();
Flame.bgm.resume();