问题描述
我正在使用,并对此做了一些自定义代码。
I've checked out this issue, and did some custom codes upon it.
void play() {
log.fine("Now playing: $_nowPlayingUrl");
_adController = VideoPlayerController.network(_nowPlayingUrl);
_adController.initialize().then((_) => setState(() {}));
_adController.play();
_adController.addListener(checkIfVideoFinished);
}
void checkIfVideoFinished() {
if (_adController == null ||
_adController.value == null ||
_adController.value.position == null ||
_adController.value.duration == null) return;
if (_adController.value.position.inSeconds ==
_adController.value.duration.inSeconds) {
_adController.removeListener(checkIfVideoFinished);
_adController.dispose();
// Change _nowPlayingIndex
setState(() {
_nowPlayingIndex = (_nowPlayingIndex + 1) % _totalIndex;
});
play();
}
}
但是使用此代码段会发出异常引发了另一个异常:处置后使用了VideoPlayerController。
But use this code snippet would send out an exception Another exception was thrown: A VideoPlayerController was used after being disposed.
有没有更好的方法播放和循环播放列表中的Flutter视频?
Is there a better way to play and loop a list of video in Flutter?
推荐答案
最近,我测试了视频列表示例。请在github 中检查源。我认为必须丢弃视频小部件。
Recently I tested video list example. please check the source in github FlutterVideoListSample. I think the video widget must be disposed.
对于我来说,我在初始化之前清除了旧的VideoPlayerController。而且我不使用 chewie
插件在进入全屏模式时创建新页面,因此无法处理下一个视频小部件。
In my case, I clear the old VideoPlayerController before initialize it. And I don't use chewie
plugin that make new page in entering fullscreen so cannot handle the next video widget.
video_player: '>=0.10.11+1 <2.0.0'
中的一些代码
some code in FlutterVideoListSample
VideoPlayerController _controller;
void _initializeAndPlay(int index) async {
print("_initializeAndPlay ---------> $index");
final clip = _clips[index];
final controller = VideoPlayerController.asset(clip.videoPath());
final old = _controller;
if (old != null) {
old.removeListener(_onControllerUpdated);
old.pause(); // mute instantly
}
_controller = controller;
setState(() {
debugPrint("---- controller changed");
});
controller
..initialize().then((_) {
debugPrint("---- controller initialized");
old?.dispose();
_playingIndex = index;
controller.addListener(_onControllerUpdated);
controller.play();
setState(() {});
});
}
这篇关于如何在Flutter中播放视频列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!