我尝试使用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);
}
}
答案 0 :(得分:0)
问题是对First()
的调用。如果您的IEnumerable
不包含任何项目,则会引发异常。
要纠正这个问题,您需要确定两件事:
DownloadUrlResolver.GetDownloadUrls()
函数返回至少1个项目。videos
变量中至少有一项将CanExtractAudio
设置为true
。