changeSource method

Future<void> changeSource({
  1. required VideoSource source,
  2. required String name,
  3. bool inheritPosition = true,
  4. bool autoPlay = true,
})

The source.video must be initialized previously

inheritPosition has the function to inherit last controller values. It's useful on changed quality video.

For example:

  _video.seekTo(lastController.value.position);

Implementation

Future<void> changeSource({
  required VideoSource source,
  required String name,
  bool inheritPosition = true,
  bool autoPlay = true,
}) async {
  final double speed = _video?.value.playbackSpeed ?? 1.0;
  final double volume = _video?.value.volume ?? 1.0;
  final Duration lastPositon = _video != null ? position : Duration.zero;

  //Change the subtitles
  if (source.subtitle != null) {
    final subtitle = source.subtitle![source.intialSubtitle];
    if (subtitle != null) {
      changeSubtitle(
        subtitle: subtitle,
        subtitleName: source.intialSubtitle,
      );
    }
  }

  //Delete all ads seen
  _ads = source.ads;
  if (_ads != null) {
    for (int i = 0; i < _ads!.length; i++) {
      for (final adSeen in adsSeen) {
        if (_ads![i] == adSeen) _ads?.removeAt(i);
      }
    }
  }

  //Initialize the video
  final oldVideo = _video;
  if (oldVideo != null) {
    _isChangingSource = true;
    notifyListeners();
  }
  await source.video.initialize();
  if (oldVideo != null) {
    oldVideo.removeListener(_videoListener);
    await oldVideo.pause();
    await oldVideo.dispose();
  }
  _video = source.video;
  source.video.addListener(_videoListener);
  _activeSourceName = name;
  _duration = endRange - beginRange;
  _isChangingSource = false;
  notifyListeners();

  //Update it inheritValues
  await _video?.setPlaybackSpeed(speed);
  await _video?.setLooping(looping);
  await _video?.setVolume(volume);

  if (inheritPosition) {
    await seekTo(lastPositon);
  } else if (source.range != null) {
    await seekTo(beginRange);
  }

  if (autoPlay) await play();
  notifyListeners();
}