这是我的视频播放器小部件,我想让视频在滚动离开视频时暂停,我想让它在滚动到视频时自动播放,这是一个社交媒体应用
class Videoplayer extends StatefulWidget { const Videoplayer({Key? key, this.width = 400, this.height = 400}) : super(key: key); final double? width; final double? height; @override _VideoplayerState createState() => _VideoplayerState(); } class _VideoplayerState extends State<Videoplayer> { late VideoPlayerController _videoPlayerController, _videoPlayerController2, _videoPlayerController3; late CustomVideoPlayerController _customVideoPlayerController; late CustomVideoPlayerWebController _customVideoPlayerWebController; final CustomVideoPlayerSettings _customVideoPlayerSettings = const CustomVideoPlayerSettings(); final CustomVideoPlayerWebSettings _customVideoPlayerWebSettings = CustomVideoPlayerWebSettings( src: longVideo, ); void initState() { super.initState(); _videoPlayerController = VideoPlayerController.network( longVideo, )..initialize().then((value) => setState(() {})); _videoPlayerController2 = VideoPlayerController.network(video240); _videoPlayerController3 = VideoPlayerController.network(video480); _customVideoPlayerController = CustomVideoPlayerController( context: context, videoPlayerController: _videoPlayerController, customVideoPlayerSettings: _customVideoPlayerSettings, additionalVideoSources: { "240p": _videoPlayerController2, "480p": _videoPlayerController3, "720p": _videoPlayerController, }, ); _customVideoPlayerWebController = CustomVideoPlayerWebController( webVideoPlayerSettings: _customVideoPlayerWebSettings, ); } @override void dispose() { _customVideoPlayerController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return SafeArea( child: ListView( children: [ kIsWeb ? Expanded( child: CustomVideoPlayerWeb( customVideoPlayerWebController: _customVideoPlayerWebController, ), ) : CustomVideoPlayer( customVideoPlayerController: _customVideoPlayerController, ), CupertinoButton( child: const Text("Play Fullscreen"), onPressed: () { if (kIsWeb) { _customVideoPlayerWebController.setFullscreen(true); _customVideoPlayerWebController.play(); } else { _customVideoPlayerController.setFullscreen(true); _customVideoPlayerController.videoPlayerController.play(); } }, ), ], ), ); } }
这是一个社交媒体应用程序,这个小部件在userpost中,所以如果用户滚动,我希望视频自动播放如果userpost包含视频,我希望视频在用户向下滚动时暂停
您可以使用NotificationListener
创建一个值来保存播放器的当前位置
double _pixels = 100;
然后在用户滚动
时使用该值进行比较NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scrollInfo) {
if (scrollInfo.metrics.pixels > _pixels) {
// User scrolled down
_customVideoPlayerWebController.pause();
} else {
// User scrolled up
_customVideoPlayerWebController.play();
}
_pixels = scrollInfo.metrics.pixels;
return false;
},
child: CustomVideoPlayerWeb(),
)