setDataSource method

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

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,
}) async {
  try {
    _autoplay = autoplay;
    _looping = looping;
    dataStatus.status.value = DataStatus.loading;

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

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

    // create a new video_player controller using the dataSource
    _videoPlayerController = _createVideoController(dataSource);
    await _initializePlayer(seekTo: seekTo);
    if (oldController != null) {
      WidgetsBinding.instance!.addPostFrameCallback((_) async {
        oldController.removeListener(this._listener);
        await oldController
            .dispose(); // dispose the previous video controller
      });
    }

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

    // set the video duration
    _duration.value = _videoPlayerController!.value.duration;

    // listen the video player events
    _videoPlayerController!.addListener(this._listener);
  } catch (e, s) {
    print(e);
    print(s);
    if (_errorText == null) {
      _errorText = _videoPlayerController!.value.errorDescription;
    }
    dataStatus.status.value = DataStatus.error;
  }
}