Flutter视频播放器未初始化

huangapple go评论84阅读模式
英文:

Flutter video player not initialized

问题

我正在尝试使用视频播放器,但似乎无法初始化。
以下是我的代码:

late VideoPlayerController _controller;

@override
void initState() {
  super.initState();
  _controller = VideoPlayerController.network(
      'https://flutter.github.io/assets-for-api- 
      docs/assets/videos/bee.mp4')
    ..initialize().then((_) {
      setState(() {});
    });
}

Widget _playView(BuildContext context) {
  if (_controller != null && _controller.value.isInitialized) {
    return Expanded(
      child: Container(
        child: VideoPlayer(_controller),
      ),
    );
  } else {
    return Text('Loading video');
  }
}
// _playView 结束

播放 `_playView` 被调用在一个 `Row()` 小部件中(在页面的主容器内)。
它总是进入 `else` 语句,因此我总是看到 'Loading video' 文本。
我没有收到任何错误。我尝试了各种来源(文档、YouTube)的代码,但都无效。
我还在 `AndroidManifest.xml` 中添加了互联网权限。
使用的插件是这个 https://pub.dev/packages/video_player
英文:

I'm trying to use Video Player but it doesn't seem to initialize.
Here is my code:

  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api- 
              docs/assets/videos/bee.mp4')
        ..initialize().then((_) {
           setState(() {});
       }
    );
  }

  Widget _playView(BuildContext context) {
    if (_controller != null && _controller.value.isInitialized) {
      return Expanded(
       child: Container(
         child: VideoPlayer(_controller),
       ),
     );
    } else {
       return Text('Loading video');
    }
  }
  //end of _playView

The play _playView is called inside A Row() widget (into the main Container of the page)
It always goes in the else statement and so i always see the 'Loading video' Text.
I don't get any errors. I've tried the code from various sources (docs, youtube) but nothing works.
I've also added the Internet permission in the AndroidManifest.xml.
The plugin used is this one https://pub.dev/packages/video_player

答案1

得分: 0

I was able to reproduce the issue when trying to play the video in the initState method after initializing the _controller:

@override
void initState() {
  super.initState();

  _controller = VideoPlayerController.network(
      'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
    ..initialize().then((_) {
      setState(() {});
      _controller.play(); // adding this
    });
}

It always goes to the else statement (return Text('Loading video');).


To resolve it, I declared a custom flag for notifying the _controller initialization instead of relying on its (_controller.value.isInitialized) property:

bool _controllerInitialized = false;

@override
void initState() {
  super.initState();

  _controller = VideoPlayerController.network(
      'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
    ..initialize().then((_) {
      setState(() {
        _controllerInitialized = true;
      });
      _controller.play();
    });
}

Hence, you could render the video player when _controllerInitialized is true:

_controllerInitialized
  ? AspectRatio( // here is your Expanded widget...
      aspectRatio: _controller.value.aspectRatio,
      child: VideoPlayer(_controller),
    )
  : const Text('Loading video'),

Note that the video will play automatically; you could stop it by removing _controller.play(); from the initState method.

英文:

I was able to reproduce the issue when trying to play the video in the initState method after initializing the _controller:

  @override
  void initState() {
    super.initState();

    _controller = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
      ..initialize().then((_) {
        setState(() {});
        _controller.play(); // adding this
      });
  }

It always goes to the else statement (return Text('Loading video');).


To resolve it, I declared a custom flag for notifying the _controller initialization instead of relying on its (_controller.value.isInitialized) property:

bool _controllerInitialized = false;

  @override
  void initState() {
    super.initState();

    _controller = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
      ..initialize().then((_) {
        setState(() {
          _controllerInitialized = true;
        });
        _controller.play();
      });
  }

Hence, you could render the video player when _controllerInitialized is true:

_controllerInitialized
          ? AspectRatio( // here is your Expanded widget...
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            )
          : const Text('Loading video'),

Note that the video will play automatically, you could stop it by removing _controller.play(); from the initState method.

huangapple
  • 本文由 发表于 2023年6月19日 20:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定