play method

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

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.0}) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  double? vol = prefs.getDouble('volValue');
  print(vol);
  final currentPlayer = audioPlayer;
  if (currentPlayer != null && currentPlayer.state != PlayerState.STOPPED) {
    currentPlayer.stop();
  }

  isPlaying = true;
  if (filename.isNotEmpty) {
    audioPlayer =
        await audioCache.loop(filename + ".mp3", volume: vol ?? 1.0);
  } else {
    print("BGM continuation");
    // String? notHome = prefs.getString("notHome");
    // audioPlayer =
    //     await audioCache.loop(notHome! + ".mp3", volume: vol ?? 1.0);
  }
}