load method

Future<void> load(
  1. EmbedInfo embedInfo
)

Load a new video with specified options to the player as identified by the embedInfo argument.

Implementation

Future<void> load(EmbedInfo embedInfo) async {
  try {
    /// Throw an Exception if Playback speed is less than 0.25x and greater than 2.25x
    embedInfo.playbackSpeedOptions?.forEach((i) {
      if (i < 0.25 || i > 2.25) {
        throw Exception('Playback speed allowed between 0.25x to 2.25x');
      }
    });

    // Renew VdoPlayerValue, remove old video related parameters
    _stopPositionTimer();
    value = VdoPlayerValue(isLoading: true);
    notifyListeners();

    Map<String, Object> loadArgs = {
      'restore': false,
      'embedInfo': embedInfo.toMap()
    };
    Map loadResultMap = await _channel!.invokeMethod("load", loadArgs);
    Map mediaInfoMap = loadResultMap['mediaInfo'];

    MediaInfo mediaInfo = MediaInfo(
        mediaId: mediaInfoMap['mediaId'],
        type: MediaInfoType.streaming,
        // TODO fix
        title: mediaInfoMap['title'],
        description: mediaInfoMap['description'],
        duration: Duration(milliseconds: mediaInfoMap['duration']));

    value = value.copyWith(
        isLoading: false,
        mediaInfo: mediaInfo,
        duration: mediaInfo.duration,
        isPlaying: loadResultMap['playWhenReady'],
        skipDuration: embedInfo.skipDuration,
        playbackSpeedOptions: embedInfo.playbackSpeedOptions);

    notifyListeners();

    if (value.isPlaying) {
      _startPositionTimer();
    }
  } on PlatformException catch (e) {
    debugPrint("load error [ ${e.code} , ${e.message!} + ]");
    String errMsg = e.message ?? "unknown error";
    VdoError error = VdoError(int.parse(e.code), errMsg, null);
    value =
        value.copyWith(isLoading: false, isBuffering: false, vdoError: error);
    notifyListeners();
  }
}