【问题标题】:Flutter: Stop playing video in list view when another video is startedFlutter:当另一个视频开始时停止在列表视图中播放视频
【发布时间】:2022-12-03 05:44:53
【问题描述】:

我正在使用 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),
    );
  }

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我想你可以使用 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 小部件用于包装列表中每个视频的 VideoContent 小部件。这将导致 VideoContent 小部件保持活动状态,并在滚动列表视图时保留其子项。开始播放新视频时,KeepAlive 小部件将处理之前的 VideoContent 小部件及其子项,停止正在播放的所有视频。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2019-02-03
      相关资源
      最近更新 更多