[英]Flutter GestureDetector not working with video_player plugin
我正在尝试实现一个功能,当点击视频本身时,视频将暂停和取消暂停。
我不想要单独的功能按钮。
当我只用文本替换 VideoPlayer 小部件时,会触发打印语句。
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return GestureDetector(
onTap: () {
setState(() {
devtools.log("Tapped");
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
child: LayoutBuilder(
builder: (context, constraints) =>
_controller.value.isInitialized
? AspectRatio(
aspectRatio: constraints.maxWidth /
constraints.maxHeight,
// _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Container()));
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return const Center(
child: CircularProgressIndicator(),
)
这是我的初始化状态
void initState() {
super.initState();
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.network(
widget.videoURL,
)..play();
// Initialize the controller and store the Future for later use.
_initializeVideoPlayerFuture = _controller.initialize();
// Use the controller to loop the video.
_controller.setLooping(true);
}
问题是您正在未来的构建器中执行一组 state ,这将触发未来再次构建,反过来它将再次初始化播放器。 我建议您在 init state 中初始化播放器并在构建方法中创建播放器..并删除未来的构建器
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.