Video playback stopped in Flutter when app goes to background, some crashes in log - Stack Overflow

I am using Flutter video_player package to play video on Android, in recent updates of Flutter or packa

I am using Flutter video_player package to play video on Android, in recent updates of Flutter or package, the app stops playing back video when I send app to background, meanwhile there are some crashes and errors in log, here is my code, logs at end of question :

class MyApp extends StatelessWidget {
      const MyApp({super.key});
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
      final String title;
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage>
        with WidgetsBindingObserver {
      int _counter = 0;
      late VideoPlayerController _controller;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      void initState() {
        super.initState();
        _controller = VideoPlayerControllerworkUrl(Uri.parse(
            '.mp4')
            ,videoPlayerOptions: VideoPlayerOptions(
              allowBackgroundPlayback: true, mixWithOthers: true),)
          ..initialize().then((_) {
            // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
            setState(() {});
          });
        WidgetsBinding.instance.addObserver(this);
      }
    
      @override
      Widget build(BuildContext context) {
        
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Center(
            child: _controller.value.isInitialized
                ? AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer (_controller),
            )
                : Container(),
          ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                setState(() {
                  _controller.value.isPlaying
                      ? _controller.pause()
                      : _controller.play();
                });
              },
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
              ),
            ),
        );
      }
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.paused) {
          print("AppLifecycleState.paused");
        }
      }
    }

And here is log of device:

2025-03-13 10:04:21.783  8186-8186  flutter                 com.example.untitled4                I  AppLifecycleState.paused
2025-03-13 10:04:21.784  8186-8186  SurfaceView@6b4b083     com.example.untitled4                D  updateSurface: surface is not valid

2025-03-13 10:04:21.863 14716-15805 WindowManager           system_server                        E  win=Window{fd23590 u0 com.example.untitled4/com.example.untitled4.MainActivity} destroySurfaces: appStopped=true cleanupOnResume=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8 caller=com.android.server.wm.ActivityRecord.destroySurfaces:7033 com.android.server.wm.ActivityRecord.destroySurfaces:7014 com.android.server.wm.ActivityRecord.activityStopped:7747 com.android.server.wm.ActivityClientController.activityStopped:321 android.app.IActivityClientController$Stub.onTransact:702 com.android.server.wm.ActivityClientController.onTransact:186 android.os.Binder.execTransactInternal:1380 
2025-03-13 10:04:21.863 14716-15805 WindowManager           system_server                        I  Destroying surface Surface(name=com.example.untitled4/com.example.untitled4.MainActivity$_8186)/@0x709f3e3 called by com.android.server.wm.WindowStateAnimator.destroySurface:811 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:478 com.android.server.wm.WindowState.destroySurfaceUnchecked:4219 com.android.server.wm.WindowState.destroySurface:4193 com.android.server.wm.ActivityRecord.destroySurfaces:7033 com.android.server.wm.ActivityRecord.destroySurfaces:7014 com.android.server.wm.ActivityRecord.activityStopped:7747 com.android.server.wm.ActivityClientController.activityStopped:321 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744716777a4589666.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信