loadVideo method

void loadVideo({
  1. String? videoUrl,
  2. bool? autoplay,
  3. bool? loop,
})

加载视频 videoUrl 视频地址 autoplay 是否自动播放

Implementation

void loadVideo({String? videoUrl, bool? autoplay, bool? loop}) {
  String videoSrc = '';
  if (videoUrl!.isEmpty) {
    videoSrc = src;
  } else {
    videoSrc = videoUrl;
  }
  src = videoSrc;

  // 判断是否是直播
  // FFprobeKit.getMediaInformation(_src).then((session) {
  //   final _duration = session.getMediaInformation()?.getDuration();
  //   _isLive =
  //       (_duration == null) || (double.parse(_duration.toString()) == 0.0);
  //   // FFmpegKitConfig.selectDocumentForRead('*/*').then((uri) {
  //   //   print('>........>>>############## $uri');
  //   // });
  //   _isReady = true;
  //   notifyListeners();
  // });

  // 初始化
  _controller = VideoPlayerController.networkUrl(
    Uri.parse(videoSrc),
    videoPlayerOptions: VideoPlayerOptions(),
  )..initialize().then((_) {
      if (autoplay!) {
        // 自动播放
        _controller.play();
      }
      if (loop!) {
        _controller.setLooping(loop);
      }
      // 获取总时长
      _duration = _controller.value.duration.inSeconds.round();
      _controller.addListener(() {
        _currentTime = _controller.value.position.inSeconds.round();

        // 获取是否正在播放
        if (_controller.value.isPlaying != _isPlaying) {
          _isPlaying = _controller.value.isPlaying;
          notifyListeners();
        }

        // 获取缓冲时间
        final buffered = _controller.value.buffered;
        if (buffered.isNotEmpty) {
          final newBufferedDuration = buffered
              .map((bufferRange) =>
                  bufferRange.end.inSeconds - bufferRange.start.inSeconds)
              .reduce((value, element) => value + element)
              .clamp(0, _controller.value.duration.inSeconds);
          if (newBufferedDuration != _bufferedTime) {
            _bufferedTime = newBufferedDuration;
            notifyListeners();
          }
        }

        // 获取是否正在加载
        if (_controller.value.isBuffering != _isLoading) {
          _isLoading = _controller.value.isBuffering;
          notifyListeners();
        }
      });
      notifyListeners();
    });
}