Flutter:当另一个视频开始时停止播放列表视图中的视频

gijlo24d  于 2022-12-05  发布在  Flutter
关注(0)|答案(2)|浏览(134)

我正在使用Better Player(https://pub.dev/packages/better_player)在列表视图中创建几个视频播放器。

ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
addAutomaticKeepAlives: true,
itemCount: awaitedContents!.length,
itemBuilder: (context, index) {
  Content content = awaitedContents[index];
 ...
  } else if (content.type == 'VIDEO') {
    return SizedBox(
      height: MediaQuery.of(context).size.width * 9 / 16,
      child: VideoContent(content.value, content.image,
          content.videoSubtitle, subtitlesEnabled),
    );
  }

如何在用户启动一个视频播放器时停止播放另一个视频播放器?

mtb9vblg

mtb9vblg1#

我想您可以使用AutomaticKeepAliveClientMixinKeepAlive小部件:

ListView.builder(
  shrinkWrap: true,
  physics: const NeverScrollableScrollPhysics(),
  itemCount: awaitedContents!.length,
  itemBuilder: (context, index) {
    Content content = awaitedContents[index];
    ...
    if (content.type == 'VIDEO') {
      return KeepAlive(
        child: VideoContent(content.value, content.image,
            content.videoSubtitle, subtitlesEnabled),
      );
    }
  }
)

KeepAlive小部件用于 Package 列表中每个视频的VideoContent小部件。这将使VideoContent小部件保持活动状态,并在滚动列表视图时保留其子小部件。当启动新视频时,KeepAlive小部件将处理前一个VideoContent小部件及其子小部件,停止播放任何视频。

s3fp2yjn

s3fp2yjn2#

要在用户启动一个视频播放器时停止另一个视频播放器的播放,可以使用BetterPlayerController类的dispose方法。此方法将停止视频播放器并释放它正在使用的所有资源。
下面是一个如何更新代码以使用dispose方法的示例:

import 'package:better_player/better_player.dart';

ListView.builder(
  shrinkWrap: true,
  physics: const NeverScrollableScrollPhysics(),
  addAutomaticKeepAlives: true,
  itemCount: awaitedContents!.length,
  itemBuilder: (context, index) {
    Content content = awaitedContents[index];
    if (content.type == 'VIDEO') {
      // Create a new BetterPlayerController instance
      BetterPlayerController playerController = BetterPlayerController();

      return SizedBox(
        height: MediaQuery.of(context).size.width * 9 / 16,
        child: VideoContent(content.value, content.image,
            content.videoSubtitle, subtitlesEnabled, playerController),
      );
    }
  }
)

class VideoContent extends StatefulWidget {
  final String videoUrl;
  final String videoSubtitle;
  final bool subtitlesEnabled;
  final BetterPlayerController playerController;

  const VideoContent(this.videoUrl, this.videoImage, this.videoSubtitle,
      this.subtitlesEnabled, this.playerController);

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

class _VideoContentState extends State<VideoContent> {
  @override
  Widget build(BuildContext context) {
    return BetterPlayer(
      widget.playerController,
      src: widget.videoUrl,
      autoplay: false,
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
      placeholder: Container(
        child: Center(
          child: CircularProgressIndicator(),
        ),
      ),
      onVideoStart: () {
        // Stop the previous video player and release its resources when the user starts a new video
        widget.playerController.dispose();
      },
    );
  }
}

在本例中,当用户启动一个新视频时,onVideoStart回调被触发,并在先前的BetterPlayerController示例上调用dispose方法,这将停止先前的视频播放器并释放其资源。

相关问题