【问题标题】:Flutter video_player plugin - VideoPlayerController.seekToFlutter video_player 插件 - VideoPlayerController.seekTo
【发布时间】:2019-11-20 22:53:46
【问题描述】:

我是 Flutter 新手,我正在使用 Flutter video_player 插件播放视频。捕获视频文件(使用相机插件)后,我想在视频中开始播放 1 秒(基本上是跳过第一秒)。我尝试使用 .seekTo 并将其传递给 Duration 但它返回以下错误:

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

API 参考看起来很简单,但它不像 Duration。这是我的代码,提前致谢!

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

}

【问题讨论】:

  • 你在寻找之前试过await videoPlayerController.initialize();吗?
  • 我只是按照下面的回复做了,它奏效了。谢谢。

标签: android ios flutter dart


【解决方案1】:

您需要先初始化,然后执行 seekTo 并播放
代码sn-p

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

演示跳过 3 秒

完整代码

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"),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

【讨论】:

  • 那行得通。谢谢!我有一个后续问题:我有 .setLooping = true。当视频开始时,它从我想要的 .seekTo 值开始。但是,当它循环时,循环从视频的原始开头开始,而不是 .seekTo 值(这是我更喜欢的)。可以这样做吗?
  • 你可以用github.com/flutter/flutter/issues/21929检查视频是否结束,然后重新搜索。或发布另一个问题。也许有好办法。
猜你喜欢
  • 2022-08-05
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2021-08-05
  • 2021-04-04
  • 2021-07-22
相关资源
最近更新 更多