【问题标题】:How to auto pause video when scrolling / when the player is not visible on screen in flutter如何在滚动/当播放器在屏幕上不可见时自动暂停视频
【发布时间】:2021-10-15 14:39:38
【问题描述】:

我正在使用一个名为“轻弹视频播放器”的视频播放器。我可以使用默认功能很好地播放视频。当我向下滚动屏幕并且视频继续在后台播放时,就会出现问题。我想在它不可见时暂停它,或者当用户导航到项目应用程序上的不同页面时。

我正在使用的视频播放器 (flick_video_player) 将 video_player 作为其依赖项。

非常感谢您的回答。 问候

【问题讨论】:

  • (好吧,如果你发布了一些代码,我们就不需要猜测细节)如果你使用ListView.builder,你可以在 dispose 方法中暂停你的视频(再次,假设你的视频被包裹在有状态的小部件。)

标签: flutter android-studio dart video video-player


【解决方案1】:

我认为您可以为此目的使用可见性检测器-

VisibilityDetector(
                            key: ObjectKey(flickManager),
                            onVisibilityChanged: (visibility){
                              if (visibility.visibleFraction == 0 && this.mounted) {
                                flickManager?.flickControlManager?.pause();//pausing  functionality 
                              }

                            },
                            child: Container(
                              child: AspectRatio(
                                aspectRatio: 1280/720,
                                child: FlickVideoPlayer(
                                    flickManager: flickManager
                                ),
                                /*VideoPlayer(
                                    video_controller
                                ),*/
                              ),
                            ),
                          ),

我正在做类似的事情。有关如何再次播放等更多信息,您可以参考此 repo-https://github.com/GeekyAnts/flick-video-player/tree/master/example/lib/feed_player

希望对您有所帮助!

【讨论】:

  • 问题以类似的方式解决。谢谢。
【解决方案2】:

也许这个可见性检测器包可以帮助https://pub.dev/packages/visibility_detector

【讨论】:

  • 感谢您的建议,我已经在使用可见性检测器了。不得不在这里和那里做一些调整:)
【解决方案3】:

NotificationListener 包裹您的视频列表,然后听听用户是开始滚动还是停止滚动。使用此值来播放或暂停您的视频。

编辑:误读了您的问题。一旦用户滚动,这将适用于暂停。如果要检测视频是否在当前视图中,请查看ScrollablePositionedList

return NotificationListener(
  onNotification: (notificationInfo) {
    if (notificationInfo is ScrollStartNotification) {
      // Set a state value to indicate the user is scrolling
    }
    if (notificationInfo is ScrollEndNotification) {
      // Set a state value to indicate the user stopped scrolling
    }
    return true;
  },
  child: YourVideos(),
);

【讨论】:

  • 感谢您的建议,这可能是一种非常直观的解决方法。但是,我能够通过查看其中一个依赖项的示例来解决它,并且必须进行一些调整:) 干杯!
  • 我什至不知道 VisibilityDetector 是一个东西,似乎是一个非常方便的工具,谢谢!
  • @Rohit 你能提供一个关于你是如何解决这个问题的提示吗?
【解决方案4】:

这正是你需要的,inview_notifier_list

InViewNotifierList(
  isInViewPortCondition:
      (double deltaTop, double deltaBottom, double vpHeight) {
    return deltaTop < (0.5 * vpHeight) && deltaBottom > (0.5 * vpHeight);
  },
  itemCount: 10,
  builder: (BuildContext context, int index) {
    return InViewNotifierWidget(
      id: '$index',
      builder: (BuildContext context, bool isInView, Widget child) {
        return Container(
          height: 250.0,
          color: isInView ? Colors.green : Colors.red,
          child: Text(
            isInView ? 'Is in view' : 'Not in view',
          ),
        );
      },
    );
  },
);

【讨论】:

  • 感谢您的建议。在查看了dependecy的示例之后,我在这里和那里做了一些调整并且能够解决它:)。如果我将来遇到困难,肯定会尝试你的方法......干杯
猜你喜欢
  • 1970-01-01
  • 2017-08-28
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
相关资源
最近更新 更多