简体   繁体   English

Flutter video_player 插件 - VideoPlayerController.seekTo

[英]Flutter video_player plugin - VideoPlayerController.seekTo

I'm new to flutter and I'm using flutter video_player plugin to play videos.我是 flutter 的新手,我正在使用 flutter video_player 插件播放视频。 After I capture a video file (using camera plugin), I'd like to start video playback 1 second into the video (essentially to skip the first second).捕获视频文件(使用相机插件)后,我想在视频的 1 秒内开始播放视频(基本上是跳过第一秒)。 I tried using.seekTo and passed it a Duration but it returned the following error:我尝试了 using.seekTo 并传递了一个 Duration 但它返回了以下错误:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The getter '_duration' was called on null.
Receiver: null
Tried calling: _duration

The API reference seemed straightforward enough but it's not like Duration. API 参考似乎很简单,但它不像 Duration。 Here's my code and thanks in advance!这是我的代码,提前致谢!

Future<void> _startVideoPlayer() async {

    videoPlayerController = VideoPlayerController.file(File(videoPath));

    videoPlayerController.setVolume(1.0);
    videoPlayerController.setLooping(true);
    videoPlayerController.seekTo(Duration(seconds: 1));
    await videoPlayerController.initialize();
    await videoPlayerController.play();

}

You need to initialize first then do seekTo and play您需要先初始化,然后执行 seekTo 并播放
code snippet代码片段

_videoPlayerController1..initialize().then((_){
                      setState(() {
                        _videoPlayerController1.seekTo(Duration(seconds: 3));
                        _videoPlayerController1.play();
                      });
                    });

demo skip 3 seconds演示跳过 3 秒

在此处输入图像描述

full code完整代码

import 'package:chewie/chewie.dart';
import 'package:chewie/src/chewie_player.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: ChewieDemo(title: 'Flutter Demo Home Page'),
    );
  }
}

class ChewieDemo extends StatefulWidget {
  ChewieDemo({this.title = 'Chewie Demo'});

  final String title;

  @override
  State<StatefulWidget> createState() {
    return _ChewieDemoState();
  }
}

class _ChewieDemoState extends State<ChewieDemo> {
  TargetPlatform _platform;
  VideoPlayerController _videoPlayerController1;
  VideoPlayerController _videoPlayerController2;
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController1 = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
    _videoPlayerController2 = VideoPlayerController.asset(
        'assets/videos/sample.mp4');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      aspectRatio: 3 / 2,
      autoPlay: false,
      looping: true,
      // Try playing around with some of these other options:

      // showControls: false,
      // materialProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
      // placeholder: Container(
      //   color: Colors.grey,
      // ),
      // autoInitialize: true,
    );
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();
    _videoPlayerController2.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: widget.title,
      theme: ThemeData.light().copyWith(
        platform: _platform ?? Theme.of(context).platform,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: Center(
                child: Chewie(
                  controller: _chewieController,
                ),
              ),
            ),
            FlatButton(
              onPressed: () {
                _chewieController.enterFullScreen();
              },
              child: Text('Fullscreen'),
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _chewieController.dispose();
                        /*_videoPlayerController2.pause();
                        _videoPlayerController2.seekTo(Duration(seconds: 0));*/

                        _chewieController = ChewieController(
                          videoPlayerController: _videoPlayerController1,
                          aspectRatio: 3 / 2,
                          autoPlay: false,
                          looping: true,
                        );

                        _videoPlayerController1..initialize().then((_){
                          setState(() {
                            _videoPlayerController1.seekTo(Duration(seconds: 3));
                            _videoPlayerController1.play();
                          });
                        });

                      });
                    },
                    child: Padding(
                      child: Text("Seek To"),
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                    ),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _chewieController.dispose();
                        _videoPlayerController2.pause();
                        _videoPlayerController2.seekTo(Duration(seconds: 0));
                        _chewieController = ChewieController(
                          videoPlayerController: _videoPlayerController1,
                          aspectRatio: 3 / 2,
                          autoPlay: true,
                          looping: true,
                        );
                      });
                    },
                    child: Padding(
                      child: Text("Video 1"),
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                    ),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _chewieController.dispose();
                        _videoPlayerController1.pause();
                        _videoPlayerController1.seekTo(Duration(seconds: 0));
                        _chewieController = ChewieController(
                          videoPlayerController: _videoPlayerController2,
                          aspectRatio: 3 / 2,
                          autoPlay: true,
                          looping: true,
                        );
                      });
                    },
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                      child: Text("Video 2"),
                    ),
                  ),
                )
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _platform = TargetPlatform.android;
                      });
                    },
                    child: Padding(
                      child: Text("Android controls"),
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                    ),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    onPressed: () {
                      setState(() {
                        _platform = TargetPlatform.iOS;
                      });
                    },
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 16.0),
                      child: Text("iOS controls"),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Flutter 中的 video_player 插件获取当前帧? - Get current Frame from video_player plugin in Flutter? 如何使用 flutter video_player 插件支持来自 ESP32CAM 的视频流? - How to support video streaming from ESP32CAM using flutter video_player plugin? Video_player 在 Flutter 中崩溃 Android 仿真器 - Video_player Crashes Android Emulator in Flutter 从页面导航到另一个页面时如何暂停颤动视频(video_player 插件) - How to pause flutter video(video_player plugin) when navigating from page to another Flutter video_player 无法播放文件中的一些视频 - Flutter video_player unable to play a few videos from file Flutter video_player 无法在一页加载多个视频 - Flutter video_player cannot load multiple videos in one page 你能在 flutter 中设置 video_player package 的样式吗? - Can you style video_player package in flutter? 发布APK video_player Flutter 中的视频替换为透明白屏 - Video replaced with a transparent white screen in the release APK video_player Flutter “配置根项目'video_player'时出现问题” - “A problem occurred configuring root project 'video_player'” 我想将视频分享到 whatsapp thta 视频正在我的应用程序中的 video_player 活动中播放 - i want to share video to whatsapp thta video is playing inside my app in video_player activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM