我正在使用 Flutter 开发一个应该在其中播放视频的桌面应用程序,因此我通过添加以下内容将video_player flutter 包添加到我的 pubspec.yaml 文件中:
dependencies:
flutter:
sdk: flutter
video_player: ^2.1.1
以下代码用于构建小部件:
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
@override
void initState() {
// Create an store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.asset("videos/media.mp4");
_initializeVideoPlayerFuture = _controller.initialize();
super.initState();
}
@override
void dispose() {
// Ensure disposing of the VideoPlayerController to free up resources.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
height: 450,
width: 800,
// Use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
child: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
_controller.play();
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: 16 / 9,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
但是,它最终出现以下错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
我知道这里已经回答了同样的问题,但是由于不支持的 .mov 文件以及由于使用了旧版本的软件包而导致的错误,我已经验证这两种情况在我的情况下都是正确的.