小编典典

如何在Flutter中播放视频列表?

flutter

我正在使用flutter
video_player包播放视频列表。

List sourceList;

sourceList = [
  {
    "size": 69742504,
    "name": "lucky-roulette.mp4",
    "mimetype": "video/mp4",
  },
  {
    "size": 69742504,
    "name": "BigBuckBunny.mp4",
    "mimetype": "video/mp4",
  }
];

我已经检查了这个问题,并对其进行了一些自定义代码。

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();
  }
}

但是使用此代码段会发出异常 Another exception was thrown: A VideoPlayerController was used after being disposed.

有更好的方法在Flutter中播放和循环播放视频列表吗?


阅读 1179

收藏
2020-08-13

共1个答案

小编典典

最近,我测试了视频列表示例。请检查github
FlutterVideoListSample中的源代码。我认为必须丢弃视频小部件。

就我而言,我在初始化之前清除了旧的VideoPlayerController。而且chewie,在进入全屏模式时,我不会使用会创建新页面的插件,因此无法处理下一个视频小部件。

依存关系

video_player: '>=0.10.11+1 <2.0.0'

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(() {});
    });
}
2020-08-13