2016-09-14 130 views
1

我正在編寫代碼從YouTube下載視頻 ,我正在使用videolibrary來做到這一點。如何使用C#將ProgressBar連接到下載任務?用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!"; 
      } 
     } 
    } 
+0

的代碼片段看看系統.CompnentModel.Backgroundworker。您可以將下載移動到後臺線程並獲得自動更新功能(由後臺工作人員報告進度)。 Google教授提供了大量樣本。嘗試一下,如果你卡住了,讓我們知道。 –

回答

0

進步改變了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(); 
+0

好的,但代碼不能保存擴展文件,如mp4,mpeg ......或任何擴展 – mark

+0

You van Chance視頻類型> VideoType.Mp4 –