Business.DownloadBusiness.DownloadVideoAsync C# (CSharp) Метод

DownloadVideoAsync() приватный Метод

private DownloadVideoAsync ( Business.DownloadItem downloadInfo, Business.DownloadItem fileInfo, EventHandler callback ) : Task
downloadInfo Business.DownloadItem
fileInfo Business.DownloadItem
callback EventHandler
Результат Task
        private async Task DownloadVideoAsync(DownloadItem downloadInfo, DownloadItem.FileProgress fileInfo, EventHandler<DownloadCompletedEventArgs> callback) {
            downloadInfo.Status = DownloadStatus.Downloading;
            VideoDownloader YTD = new VideoDownloader(fileInfo.Source, fileInfo.Destination);
            YTD.DownloadProgressChanged += (sender, e) => {
                if (downloadInfo.IsCanceled)
                    e.Cancel = true;
                else {
                    fileInfo.BytesTotal = YTD.DownloadSize;
                    fileInfo.BytesDownloaded = e.ProgressBytes;
                    downloadInfo.UpdateProgress();
                }
            };

            // Run downloader task.
            await Task.Run(() => {
                try {
                    YTD.Execute();
                } catch {
                    downloadInfo.Status = DownloadStatus.Failed;
                }
            }).ConfigureAwait(false);

            // Detect whether this is the last file.
            fileInfo.Done = true;
            if (downloadInfo.Files.Any(d => !d.Done) == false) {
                var NextDownload = StartNextDownloadAsync().ConfigureAwait(false);

                // Raise events for the last file part only.
                if (downloadInfo.IsCompleted) {
                    try {
                        await DownloadCompletedAsync(downloadInfo).ConfigureAwait(false);
                    } catch {
                        downloadInfo.Status = DownloadStatus.Failed;
                    }
                } else if (downloadInfo.IsCanceled)
                    DownloadCanceled(downloadInfo);
                RaiseCallback(downloadInfo);

                await NextDownload;
            }
        }

Same methods

DownloadBusiness::DownloadVideoAsync ( Media video, int queuePos, EventHandler callback ) : Task
DownloadBusiness::DownloadVideoAsync ( Media video, int queuePos, bool upgradeAudio, EventHandler callback ) : Task

Usage Example

        /// <summary>
        /// Prepares for playing the next video.
        /// </summary>
        /// <param name="queuePos">The video position to select. 0 for current, 1 for next.</param>
        /// <param name="attempts">The number of attemps already made, to avoid infinite loop.</param>
        /// <returns>Whether the file is downloading.</returns>
        private async Task <bool> PrepareNextVideoAsync(int queuePos, int attempts)
        {
            bool FileExists = false;

            if (nextVideo != null)
            {
                FileExists = File.Exists(Settings.NaturalGroundingFolder + nextVideo.FileName);
            }

            if (!FileExists)
            {
                // If file doesn't exist and can't be downloaded, select another one.
                if (!Settings.SavedFile.AutoDownload || nextVideo == null || nextVideo.DownloadUrl.Length == 0)
                {
                    await SelectNextVideoAsync(queuePos, false, attempts + 1).ConfigureAwait(false);
                }
                // If file doesn't exist and can be downloaded, download it.
                else if (nextVideo != null && nextVideo.DownloadUrl.Length > 0)
                {
                    Application.Current.Dispatcher.Invoke(() => PlaylistChanged?.Invoke(this, new EventArgs()));
                    await downloadManager.DownloadVideoAsync(nextVideo, queuePos, Download_Complete).ConfigureAwait(false);

                    return(true);
                }
            }
            return(false);
        }
All Usage Examples Of Business.DownloadBusiness::DownloadVideoAsync