我正在构建 Flutter 视频播放器应用程序,并在尝试播放视频时遇到以下错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
PlatformException(VideoError, Video player had error
com.google.android.exoplayer2.ExoPlaybackException:
Source error, null, null)
代码(最小可重现示例):
class VideoInfo extends StatefulWidget {
// ... other code ...
class _VideoInfoState extends State<VideoInfo> {
late VideoPlayerController _controller;
@override
Widget build(BuildContext context) {
return Scaffold(
body:
// ... other code ...
Widget _playView(BuildContext context){
final controller =_controller;
if(controller!=null&&controller.value.isInitialized){
return Container(
height: 300,
width: 300,
child: VideoPlayer(controller),
);
}else{
return Text("Being initialized pls wait");
}
}
_onTapVideo(int index){
final controller = VideoPlayerController.network(videoInfo[index]["videoUrl"]);
_controller= controller;
setState(() {
});
我尝试过的:
video_player
依赖项。android.permission.INTERNET
添加到 Android 清单。您正在为同一变量赋值。
使用,
_controller = controller;
而不是,
_controller=_controller;
当你使用late时,这意味着变量可以延迟初始化,所以你需要先初始化它,然后才能使用它。你可以在_onTapVideo函数中中断调试,看看你的_controller=_controller,是否真的初始化了?