从youtube下载音频c#

时间:2016-12-05 22:24:06

标签: c# winforms youtube

我尝试使用YoutubeExtractor从youtube下载音频但是如果点击按钮我有信息"序列没有元素"。我怎么解决它?

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(textBox1.Text);

        VideoInfo video = videos.Where(info => info.CanExtractAudio).OrderByDescending(info => info.AudioBitrate).First();

        if (video.RequiresDecryption)
        {
            DownloadUrlResolver.DecryptDownloadUrl(video);
        }
        AudioDownloader download = new AudioDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.AudioExtension));

        download.Execute();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
} 

1 个答案:

答案 0 :(得分:0)

问题是对First()的调用。如果您的IEnumerable不包含任何项目,则会引发异常。

要纠正这个问题,您需要确定两件事:

  1. DownloadUrlResolver.GetDownloadUrls()函数返回至少1个项目。
  2. videos变量中至少有一项将CanExtractAudio设置为true