【问题标题】:Show download progress in a ProgressBar with C#使用 C# 在 ProgressBar 中显示下载进度
【发布时间】:2016-09-14 07:46:14
【问题描述】:

我正在编写代码以从 YouTube 下载视频 我正在使用 videolibrary 来做到这一点。如何使用 C# 将下载任务与 ProgressBar 连接起来?

这是我目前的代码:

private async void buttondownload_Click(object sender, EventArgs e)
{
    try
    {
        using (FolderBrowserDialog fbd = new FolderBrowserDialog() { Description = "select your path ." })
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {                        
                var youtube = YouTube.Default;
                labelstatus.Text = "Downloading....";
                var video = await youtube.GetVideoAsync(textBoxurl.Text);
                //setting progress bar...............................??????

                File.WriteAllBytes(fbd.SelectedPath + video.FullName, await video.GetBytesAsync());
                labelstatus.Text = "Completed!";
            }
        }
    }

【问题讨论】:

  • 看看 System.CompnentModel.Backgroundworker。您可以将下载移动到后台线程并获得自动更新功能(通过后台工作人员报告进度)。谷歌教授提供了大量的样本。试试看,如果遇到困难,请告诉我们。

标签: c# youtube progress-bar


【解决方案1】:

视频库不支持进度更改 但它在YoutubeExtractor 见下面的代码sn-p

Thread youtubeDownloader = new Thread(delegate () 
        {

            FolderBrowserDialog b = new FolderBrowserDialog();

            this.Invoke((MethodInvoker)delegate () { if (b.ShowDialog() != DialogResult.OK) return;  });
                {
                    string link = textBox1.Text;
                    IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
                    VideoInfo video = videoInfos
                        .First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);
                    if (video.RequiresDecryption)
                    {
                        DownloadUrlResolver.DecryptDownloadUrl(video);
                    }

                    var videoDownloader = new VideoDownloader(video, b.SelectedPath + video.Title);
                    videoDownloader.DownloadProgressChanged += (sender1, args) => this.Invoke((MethodInvoker)delegate () { progressBar1.Value = (int)args.ProgressPercentage;});
                    videoDownloader.Execute();
                }

        });
        youtubeDownloader.IsBackground = true;
        youtubeDownloader.Start();

【讨论】:

  • 好的,但是代码无法保存扩展文件,例如mp4,mpeg......或任何扩展
  • You van Chance 视频类型> VideoType.Mp4
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多