startVideoDownload method

void startVideoDownload()

启动视频下载流程

Implementation

void startVideoDownload() {
  // 没有数据源
  if (_widgetDataGetter() == null) {
    logw(
      "Download skipped: player not configured. "
      "Call configure() before startVideoDownload().",
      tag: _logTag,
    );
    return;
  }

  // 没有设置保存路径
  if (savePath == null || savePath!.isEmpty) {
    _showError(PlayerI18n.t(PlayerI18nKeys.downloadStoragePathNotSet));
    return;
  }

  final currentState = stateNotifier.value;
  if (currentState is DownloadCompletedState) {
    _showSuccess(
      PlayerI18n.t(
        PlayerI18nKeys.downloadAlreadySaved,
        {"path": currentState.downloadFile},
      ),
    );
    return;
  }
  if (currentState is DownloadingState) {
    _showInfo(
      PlayerI18n.t(
        PlayerI18nKeys.downloadProgress,
        {"progress": currentState.progress},
      ),
    );
    return;
  }

  _downloader.setSaveDir(savePath!);

  final prepared = _prepareDownload();
  if (prepared == null) {
    stateNotifier.value = const DownloadErrorState(
      errorCode: "SOURCE_TYPE_UNSUPPORTED",
      errorMessage: "Download only supports VidSts or VidAuth source. "
          "UrlSource does not provide vid for download. "
          "Please switch to VidSts/VidAuth video source.",
    );
    return;
  }

  final (vid, downloadAction) = prepared;
  downloadAction.then((value) {
    _showInfo(PlayerI18n.t(PlayerI18nKeys.downloadStart));
    _executeDownloadWithMediaInfo(vid, value);
  }).catchError((error, stack) {
    _handleDownloadError(error);
  });
}