我正在 Xamarin / C# 中为 Android 创建一个 Youtube 到 MP3 下载器。我正在使用 VideoLibrary 下载视频。我的代码:
if (!string.IsNullOrEmpty(EditText.Text))
{
byte[] bytes = null;
try
{
var v = YouTube.Default.GetVideo(EditText.Text);
bytes = await webClient.DownloadDataTaskAsync(v.Uri);
}
catch (TaskCanceledException)
{
Toast.MakeText(this, "Task Canceled", ToastLength.Long).Show();
return;
}
catch (Exception a)
{
Toast.MakeText(this, a.InnerException.Message, ToastLength.Long).Show();
Dialog.Progress = 0;
return;
}
var video = YouTube.Default.GetVideo(EditText.Text).Title;
var documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/Download";
string localFilename = video
.Replace('.', ' ')
.Replace('-', ' ')
.Replace('(', ' ')
.Replace(')', ' ')
.Replace('"', ' ')
.Replace(',', ' ');
string localPath = System.IO.Path.Combine(documentsPath, localFilename);
Dialog.SetTitle("Download Complete");
FileStream fs = new FileStream(localPath + ".mp3", FileMode.OpenOrCreate);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
所以这行得通,我可以将视频下载为 mp3 文件,但它也可以下载视频。当我然后单击 .mp3 文件时,它也会下载视频。问题在于,我无法将其添加到播放列表中,因为它是 mp3 格式的视频,而不是声音。
SO:我可以只获得 VideoLibrary 中的声音还是可以轻松地将这个视频文件转换为 mp3 文件?
谢谢你的帮助!!
注意:它必须在 Android (Xamarin) 上工作