startVideoDownload method
void
startVideoDownload()
启动视频下载流程
根据视频源类型初始化并开始下载视频。支持两种授权类型:
- STS授权方式 (VidStsVideoSource)
- 授权码方式 (VidAuthVideoSource)
下载前会检查视频是否已下载,避免重复下载。
Implementation
void startVideoDownload() {
// 检查视频源是否存在
if (_widgetData?.videoSource == null) return;
// 检查下载保存目录是否存在
if (downloadSavePath == null || downloadSavePath!.isEmpty) {
SnackBarUtil.error(
_context,
"Please set AliPlayerWidgetGlobalSetting.setStoragePaths first.",
);
return;
}
// 检查是否已经下载完成,如果已完成则提示用户
if (downloadStateNotifier.value is DownloadCompletedState) {
final DownloadCompletedState state =
downloadStateNotifier.value as DownloadCompletedState;
SnackBarUtil.success(_context,
"This video has already been downloaded. SavePath: ${state.downloadFile}");
return;
} else if (downloadStateNotifier.value is DownloadingState) {
// 如果处于加载状态,直接return
return;
}
// 设置下载保存目录
_aliDownloader.setSaveDir(downloadSavePath!);
String? vid; // 视频ID
Future<dynamic>? downloadAction;
// 根据不同视频源类型执行相应下载逻辑
if (_widgetData?.videoSource is VidStsVideoSource) {
// 处理 STS 类型视频源的下载准备
final stsSource = _widgetData?.videoSource as VidStsVideoSource;
vid = stsSource.vid;
String accessKeyId = stsSource.accessKeyId;
String accessKeySecret = stsSource.accessKeySecret;
String securityToken = stsSource.securityToken;
downloadAction = _aliDownloader.prepare(
FlutterAvpdef.DOWNLOADTYPE_STS,
vid,
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
securityToken: securityToken,
);
} else if (_widgetData?.videoSource is VidAuthVideoSource) {
// 处理 Auth 类型视频源的下载准备
final authSource = _widgetData?.videoSource as VidAuthVideoSource;
vid = authSource.vid;
String playAuth = authSource.playAuth;
downloadAction = _aliDownloader.prepare(
FlutterAvpdef.DOWNLOADTYPE_AUTH,
vid,
playAuth: playAuth,
);
}
if (vid != null && vid.isNotEmpty) {
downloadAction?.then((value) {
_executeDownloadWithMediaInfo(vid!, value);
}).catchError((error, stack) {
_handleDownloadError(error);
});
} else {
// UrlVideoSource is not support;
downloadStateNotifier.value = const DownloadErrorState(
errorCode: "Video Source Error",
errorMessage: "Unsupported video source. Only vid source is supported.",
);
}
}