用c#在进度条中显示下载进度
本文关键字:显示 下载 | 更新日期: 2023-09-27 18:06:51
我正在编写从YouTube下载视频的代码我使用videolibrary来做到这一点。如何将下载任务与c#的进度条连接起来?
这是我到目前为止的代码:
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!";
}
}
}
videolibrary不支持进度更改但它是在YoutubeExtractor参见下面的代码片段
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();