英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论