chewie icon indicating copy to clipboard operation
chewie copied to clipboard

When playing multiple videos, playing one of them will pause the others

Open Yanren1225 opened this issue 2 years ago • 2 comments

Video:

https://user-images.githubusercontent.com/25337480/132444214-3dc1e23b-2123-430a-8876-7ea6be5aabb6.mp4

I put Chewie inside the ListView, like this.

Container(
              height: (210 * chewieControllers.length).h,
              child: ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: chewieControllers.length,
                  itemBuilder: (context, index) {
                    return Container(
                      height: 200.h,
                      decoration: BoxDecoration(
                        color: Colors.grey,
                      ),
                      child: Chewie(
                        controller: chewieControllers[index],
                      ),
                    );
                  }),
            )

The videoPlayerControllers and chewieControllers were added by me in a loop, like this.

  void videoPlayerInit() {
    List<String> videoList = [
      /// links...
    ];

    for (var videoUrl in videoList) {
      var tempVideoPlayerController = VideoPlayerController.network(videoUrl);
      videoPlayerControllers.add(tempVideoPlayerController);

      var tempChewieController = ChewieController(
        videoPlayerController: tempVideoPlayerController,
        aspectRatio: 3 / 2,
        autoPlay: false,
        looping: false,
        autoInitialize: true,
        allowPlaybackSpeedChanging: false
      );

      tempChewieController.addListener(() {
        //判断是否全屏
        if (!tempChewieController.isFullScreen) {
          SystemChrome.setPreferredOrientations([
            // 强制竖屏
            DeviceOrientation.portraitUp,
            DeviceOrientation.portraitDown
          ]);
        }
      });

      chewieControllers.add(tempChewieController);
    }
  }

Then I control the playback like this

PlayButton(event: (bool isPlay) {
              for (var videoPlayerController in videoPlayerControllers) {
                if(isPlay){
                  videoPlayerController.play();
                }else{
                  videoPlayerController.pause();
                }
              }
            }),

PlayButton is my custom button that returns a bool

class PlayButton extends StatefulWidget {
  final Function event;

  const PlayButton({Key key, @required this.event}) : super(key: key);

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

class _PlayButtonState extends State<PlayButton> {
  bool _isPlay = false;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ButtonStyle(
        minimumSize: MaterialStateProperty.all(Size(156, 36)),
        elevation: MaterialStateProperty.all(0),
        backgroundColor: MaterialStateProperty.all(Color(0xFFe12f36)),
        shape: MaterialStateProperty.all(
          StadiumBorder(),
        ),
      ),
      onPressed: () {
        setState(() {
          _isPlay = !_isPlay;
        });
        if (widget.event != null) {
          widget.event(_isPlay);
        }
      },
      child: Text(
        _isPlay ? '暂停' : '播放',
        style: TextStyle(
          fontSize: 14.h,
          color: Colors.white,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
}

Yanren1225 avatar Sep 08 '21 03:09 Yanren1225

Try changing your video controller to something like this:

tempVideoPlayerController = VideoPlayerController.network(
  videoUrl,
  videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
);

I use it to don't interrupt videos/audios in other apps, maybe it will work in your case too.

NANI-SORE avatar Sep 14 '21 10:09 NANI-SORE

videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),

works for me also. Thanks @NANI-SORE

slovnicki avatar Dec 23 '21 21:12 slovnicki