just_audio_background在关闭应用程序后继续播放

问题描述 投票:0回答:2

我发现即使关闭应用程序并关闭向下拖动时出现的小部件(Android),just_audio_background 仍会继续播放音频。 仅当我转到应用程序信息并单击强制关闭时,音频才会停止播放。

这种行为是预期的吗?我希望 just_audio_background 仅在应用程序位于后台时播放。也就是说,如果您关闭应用程序,它应该停止播放。

flutter just-audio
2个回答
0
投票

您必须像这样在处理功能下处理您的播放器控制器。 使用您的控制器名称而不是“_player”。试试这个。

  @override
  void dispose() {
     player.dispose();
     super.dispose();
  }

0
投票

实施这个。

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> with WidgetsBindingObserver{

  final  player = AudioPlayer();

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


  @override
  void didChangeDependencies() {

    super.didChangeDependencies();
  }

   @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }


   @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.paused) {
      // App is in the background
      print('App is in the background');
    } else if (state == AppLifecycleState.resumed) {
      print('App is resumed from background');
    } else if (state == AppLifecycleState.inactive) {
      // App is inactive
      print('App is inactive');
    } else if (state == AppLifecycleState.detached) {
     player?.stop();
     player?.dispose();
      print('App is closed');
    }
    
  }
© www.soinside.com 2019 - 2024. All rights reserved.