如何在颤动中用视频播放器显示视频

how to show video with video player in flutter

如何在flutter中显示视频?

我应该收到 api 有 url 的视频要显示,但它会显示我的进度指示器的白页, 我试了一个星期,但什么也做不了, 然后我尝试使用资产视频,但它也没有用 这是我的代码,有什么问题吗?

请帮帮我,谢谢。

class _MyHomePageState extends State<MyHomePage> {
  VideoPlayerController _videoPlayerController;
  Future<void> _initializedVideoPlayerFuture;
  String videoUrl =
      'https://storage.koolshy.co/shasha-transcoded-videos-2019/1c18ada9-a82a-4490-ad2f-87c3ba3ed251_240.mp4';
  String videoTrack = 'assets/video.mp4';
  @override
  void initState() {
    super.initState();
//    _videoPlayerController = VideoPlayerController.network(videoUrl);
    _videoPlayerController = VideoPlayerController.asset(videoTrack);
    _videoPlayerController.setLooping(true);
    _videoPlayerController.setVolume(1.0);
  }

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
  }

  void _incrementCounter() {
    setState(() {
      _videoPlayerController.value.isPlaying
          ? _videoPlayerController.pause()
          : _videoPlayerController.play();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FutureBuilder(
              future: _initializedVideoPlayerFuture,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  return AspectRatio(
                    aspectRatio: _videoPlayerController.value.aspectRatio,
                    child: VideoPlayer(_videoPlayerController),
                  );
                } else {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'play/pause',
        child: Icon(_videoPlayerController.value.isPlaying
            ? Icons.pause
            : Icons.play_arrow),
      ),
    );
  }
}

缺少语句

    _initializedVideoPlayerFuture = _videoPlayerController.initialize();

应该在initstate()

  void initState() {
    super.initState();
    _videoPlayerController = VideoPlayerController.network(videoUrl);
//    _videoPlayerController = VideoPlayerController.asset(videoTrack);
    _initializedVideoPlayerFuture = _videoPlayerController.initialize();

    _videoPlayerController.setLooping(true);
    _videoPlayerController.setVolume(1.0);
  }