play method

Future<void> play(
  1. String fileName, {
  2. double volume = 1,
  3. String? package,
})

Plays and loops a background music file specified by fileName.

The fileName is the full path of the asset, as declared in the pubspec.yaml, for example assets/audio/song.mp3.

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,
  String? package,
}) async {
  await audioPlayer.release();
  await audioPlayer.setReleaseMode(ReleaseMode.loop);
  await audioPlayer.setVolume(volume);
  final path = package == null ? fileName : 'packages/$package/$fileName';
  await audioPlayer.setSource(AssetSource(path));
  await audioPlayer.resume();
  isPlaying = true;
}