I am Creating an Youtube to MP3 Downloader for Android in Xamarin / C#. I am using the VideoLibrary to download the Video. My Code:
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();
So this works, I can download the Video as a mp3 file, but it also Downloads the Video. When I then Click on the .mp3 file, but it also downloads the Video. The Problem at this is, that I cant add this to a playlist, since its a Video in mp3 format, not a sound.
SO: Can I get only the Sound in VideoLibrary OR can I easy convert this Video file into a mp3 file?
Thanks for your help!!
Note: It has to work on Android (Xamarin)
According to the docs of the VideoLibrary, you can use that library to download the video in the same file extension as you view it. There is no published capability in VideoLibrary of changing the video format to audio, you would have to use a different library for that.
You are trying to solve this problem by simply changing the extension to MP3
instead of appropriately using the FileExtension
property provided. That's why you are seeing this issue.