Home > front end >  Flutter Video Player Automatically Pauses when overlay video appears on screen of other application
Flutter Video Player Automatically Pauses when overlay video appears on screen of other application

Time:04-08

I have a background in my application screen that plays in a loop. The video works fine when we change the screen and when we change the state of the application(pause/resume) but there is a huge problem. When I play video in some other application just like Netflix and close the Netflix app and open my application. The pop-up video appears on the phone screen and the video in my application automatically when the video is in the pop-up plays. I will show you an example of what actually happening. Please open this link I have added a screen recording of my application too. You can understand what is actually happening.

Screen Recording of the application

Here is the code I am using for my video player

late VideoPlayerController _controller;

In init state of the Widget

videoPlayerBloc = context.read<VideoPlayerBloc>();
_controller = VideoPlayerController.asset('assets/videos/intro.mp4')
  ..initialize().then((_) {
    videoPlayerBloc.add(PlayVideo());
    _controller.play();
    _controller.setLooping(true);
    setState(() {});
  });

WidgetsBinding.instance!.addObserver(
    LifecycleEventHandler(resumeCallBack: () async =>
        videoPlayerBloc.add(PlayVideo()), suspendingCallBack: () async {
    })
);

In the build method of Widget

BlocListener<VideoPlayerBloc, VideoPlayerState>(
        listener: (context, state) {
          _controller = VideoPlayerController.asset('assets/videos/intro.mp4')
            ..initialize().then((_) {
              _controller.play();
              _controller.setLooping(true);
              setState(() {});
            });
        },
        child: SizedBox.expand(
          child: FittedBox(
            fit: BoxFit.cover,
            child: SizedBox(
              width: _controller.value.size.width,
              height: _controller.value.size.height,
              child: Opacity(
                  opacity: 0.75,
                  child: VideoPlayer(_controller)),
            ),
          ),
        )
    )

Any kind of help would be much appreciated. This is my first experience with stack overflow. Hope to get some positive response from audience.

CodePudding user response:

https://pub.dev/documentation/video_player_platform_interface/latest/video_player_platform_interface/VideoPlayerOptions-class.html

You'll want to set your video player options https://pub.dev/documentation/video_player_platform_interface/latest/video_player_platform_interface/VideoPlayerOptions/mixWithOthers.html

mixWithOthers property to true to have your video continue being played :)

those options are part of the VideoPlayerController constructor

VideoPlayerController(videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),);
  • Related