颤振 |使用音频播放器最小化时停止播放音乐

Flutter | Stop music when minimised using audioplayers

在我的应用程序中,我正在循环播放音乐(本地),除非用户停止,否则它会一直播放。我正在使用音频播放器包。

Future playLoop(String filePath) async {
  player.stop();
  player = await cache.loop(filePath);
}

目前,应用程序最小化时,音乐不会停止。我想实现的功能是当应用程序最小化时,它应该停止在后台播放音乐。

提前致谢。

Solutions :

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
     //stop your audio player
    }else{
      print(state.toString());
    }
  }

 @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

There are mainly 4 states for it:

resumed: The application is visible and responding to user input.

inactive: The application is in an inactive state and is not receiving user input.

paused: The application is not currently visible to the user, not responding user input, and running in the background.

detached: The application is still hosted on a flutter engine but is detached from any host views.