简体   繁体   English

Flutter GestureDetector 不适用于 video_player 插件

[英]Flutter GestureDetector not working with video_player plugin

I'm trying to implement a feature where the video will pause and unpause when the video itself is tapped.我正在尝试实现一个功能,当点击视频本身时,视频将暂停和取消暂停。

i don't want separate buttons for the functionality.我不想要单独的功能按钮。

when i replace the VideoPlayer widget with just text, the print statement fires.当我只用文本替换 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(),
        )

here is my initState这是我的初始化状态

  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);

} }

The issue is that you are performing a set state inside a future builder which will trigger the future to build again and in turn it will initialise the player again.问题是您正在未来的构建器中执行一组 state ,这将触发未来再次构建,反过来它将再次初始化播放器。 I would suggest you to initialise the player in init state and create the player in the build method..and remove the future builder我建议您在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM