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 (windows) {
      if (_videoPlayerControllerWindows != null &&
          _videoPlayerControllerWindows!.playback.isPlaying) {
        await this.pause(notify: false);
      }
    } else {
      // 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;
    Player? oldControllerWindows = _videoPlayerControllerWindows;

    // create a new video_player controller using the dataSource
    if (windows) {
      //if(_videoPlayerControllerWindows==null){
      _videoPlayerControllerWindows =
          _createVideoControllerWindows(dataSource, seekTo);
      //}else{
      //_videoPlayerControllerWindows=setPlayerDataSource(dataSource, _videoPlayerControllerWindows!);
      //}
      //_videoPlayerControllerWindows!.seek(seekTo!);
      if (oldControllerWindows != null) {
        await removeWindowsListener();
        oldControllerWindows
            .dispose(); // dispose the previous video controller
      }
    } else {
      _videoPlayerController = await _createVideoController(dataSource);
      await _videoPlayerController!.initialize();

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

    if (windows) {
      _listener(player: _videoPlayerControllerWindows);
    } else {
      // set the video duration
      print("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(this._listener);
    }
  } catch (e, s) {
    print(e);
    print(s);
    if (_errorText == null) {
      _errorText = _videoPlayerController!.value.errorDescription;
    }
    dataStatus.status.value = DataStatus.error;
  }
}