setDataSource method

Future<void> setDataSource(
  1. DataSource dataSource, {
  2. bool autoplay = true,
  3. bool looping = false,
  4. Duration seekTo = Duration.zero,
})

set the video data source

autoPlay if this is true the video automatically start

Implementation

Future<void> setDataSource(
  DataSource dataSource, {
  bool autoplay = true,
  bool looping = false,
  Duration seekTo = Duration.zero,
}) async {
  try {
    _autoPlay = autoplay;
    _looping = looping;
    dataStatus.status.value = DataStatus.loading;

    // if we are playing a video
    if (_videoPlayerController != null &&
        _videoPlayerController!.value.isPlaying) {
      await pause(notify: false);
    }

    // save the current video controller to be disposed in the next frame
    VideoPlayerController? oldController = _videoPlayerController;

    _videoPlayerController = _createVideoController(dataSource);
    await _videoPlayerController!.initialize();

    if (oldController != null) {
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        oldController.removeListener(_listener);
        await oldController
            .dispose(); // dispose the previous video controller
      });
    }

    // set the video duration
    customDebugPrint("Duration is ${_videoPlayerController!.value.duration}");

    _duration.value = _videoPlayerController!.value.duration;

    /// notify that video was loaded
    dataStatus.status.value = DataStatus.loaded;

    await _initializePlayer(seekTo: seekTo);
    // listen the video player events
    _videoPlayerController!.addListener(_listener);
  } catch (e, s) {
    customDebugPrint(e);
    customDebugPrint(s);
    _errorText ??= _videoPlayerController!.value.errorDescription ?? "$e";
    dataStatus.status.value = DataStatus.error;
  }
}