public PlayDownloadFactory(object parameter) { _downloadInfo = parameter as DownloadInfo; }
/// <summary> /// 开始下载一个新任务,此方法为后台线程调用 /// </summary> /// <param name="info"></param> protected async void StartOneDownload(DownloadInfo info) { try { _isDownloading = true; var folder = await GetDownloadFolder(); var destinationFile = await folder. CreateFileAsync(info.LocalFileName, CreationCollisionOption.ReplaceExisting); info.LocalFileName = destinationFile.Name; var downloader = new BackgroundDownloader(); var download = downloader.CreateDownload(new Uri(info.DownloadUri), destinationFile); info.DownloadGuid = download.Guid; await HandleDownloadAsync(download, info, true); } catch (Exception ex) { _isDownloading = false; DownloadFailture(info, ex.Message); } }
private void downloadBar_Click(object sender, RoutedEventArgs e) { try { if (PersonalFactory.Instance.Logined) { var downinfos = new List<DownloadInfo>(_downloadList.Count); foreach (var downInfo in _downloadList) { var index = ChannelUtils.CreateProgramIndex(downInfo.Index, _dataSource); downInfo.IsSelected = false; downInfo.IsDownloading = true; var down = new DownloadInfo() { TypeId = _dataSource.Type, ParentId = _dataSource.Id, ParentName = _dataSource.Title, ProgramIndex = downInfo.Index, ChannelId = downInfo.ChannelId, ImageUri = _dataSource.ImageUri, DownloadState = DownloadState.Await, Title = ChannelUtils.CreateChannelTitle(index, _dataSource, false) }; down.LocalFileName = string.Format("{0}.mp4", down.Title); downinfos.Add(down); } DownloadViewModel.Instance.AddDownloads(downinfos); if (_downloadList.Count > 0) TileUtils.CreateToastNotifications("已加入下载,请到个人中心查看"); _downloadList.Clear(); downloadBar.Visibility = Visibility.Collapsed; } else { TileUtils.CreateToastNotifications("请先至个人中心登录"); } bottomBar.IsOpen = false; } catch { TileUtils.CreateToastNotifications("加入下载失败"); } }
protected abstract void DownloadProgress(DownloadOperation download, DownloadInfo info);
protected abstract void DownloadFailture(DownloadInfo info, string errMessage);
protected abstract void DownloadCompleted(DownloadInfo info);
private async Task HandleDownloadAsync(DownloadOperation download, DownloadInfo info, bool start) { try { _activeDownloads[info.ChannelId] = download; _cts[info.ChannelId] = new CancellationTokenSource(); var progressCallback = new Progress<DownloadOperation>(downOperation => { DownloadProgress(downOperation, info); }); if (start) { await download.StartAsync().AsTask(_cts[info.ChannelId].Token, progressCallback); } else { await download.AttachAsync().AsTask(_cts[info.ChannelId].Token, progressCallback); } _activeDownloads.Remove(info.ChannelId); _cts.Remove(info.ChannelId); var response = download.GetResponseInformation(); _isDownloading = false; DownloadCompleted(info); } catch (System.Threading.Tasks.TaskCanceledException) { } catch (Exception ex) { _isDownloading = false; DownloadFailture(info, ex.Message); } }
protected void CancelDownload(DownloadInfo info) { if (_cts.ContainsKey(info.ChannelId)) { _cts[info.ChannelId].Cancel(); _cts[info.ChannelId].Dispose(); } _cts.Remove(info.ChannelId); }
/// <summary> /// 继续一个下载任务 /// </summary> /// <param name="info"></param> protected bool ResumeOneDownload(DownloadInfo info) { if (_activeDownloads.ContainsKey(info.ChannelId) && _activeDownloads[info.ChannelId].Progress.Status == BackgroundTransferStatus.PausedByApplication) { _isDownloading = true; _activeDownloads[info.ChannelId].Resume(); info.DownloadState = DownloadState.Downloading; return true; } return false; }
/// <summary> /// 暂停一个下载任务 /// </summary> /// <param name="info"></param> protected void PauseOneDownload(DownloadInfo info) { if (_activeDownloads.ContainsKey(info.ChannelId) && (_activeDownloads[info.ChannelId].Progress.Status == BackgroundTransferStatus.Running || _activeDownloads[info.ChannelId].Progress.Status == BackgroundTransferStatus.PausedNoNetwork || _activeDownloads[info.ChannelId].Progress.Status == BackgroundTransferStatus.PausedCostedNetwork) ) { _activeDownloads[info.ChannelId].Pause(); info.DownloadState = DownloadState.Pause; } }