play method

Future<void> play(
  1. String filename, {
  2. double volume = 1,
})

Plays and loops a background music file specified by filename.

The volume can be specified in the optional named parameter volume where 0 means off and 1 means max.

It is safe to call this function even when a current BGM track is playing.

Implementation

Future<void> play(String filename, {double volume = 1}) async {
  final currentPlayer = audioPlayer;
  if (currentPlayer != null && currentPlayer.state != PlayerState.STOPPED) {
    currentPlayer.stop();
  }

  isPlaying = true;
  audioPlayer = await audioCache.loop(filename, volume: volume);
}