Reputation: 196
I am able to store media files into the Isolated Storage and able to retrieve the same. Now what I want is to play that file in music player in phone, but music player says "no music found". How can i show my downloaded mp3 files in isolated storage to the media library? I am using this code to download the mp3 file. Is there any way to download the file to the readable storage like (phone memory/ internal storage / memory card)???
void imgDown_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ToastPrompt toast = new ToastPrompt();
toast.Foreground = new SolidColorBrush(Colors.Black);
toast.FontSize = 20;
toast.Background = new SolidColorBrush(Colors.Yellow);
toast.Message = "Please wait";
toast.Show();
try
{
//Create a webclient that'll handle your download
WebClient client = new WebClient();
//Run function when resource-read (OpenRead) operation is completed
client.OpenReadCompleted += client_OpenReadCompleted;
//Start download / open stream
client.OpenReadAsync(new Uri(streamUrl));
}
catch (Exception ex)
{
toast.Message = "Downloading error";
toast.Show();
}
}
async void client_OpenReadCompleted(object sender,
OpenReadCompletedEventArgs e)
{
ToastPrompt toast = new ToastPrompt();
toast.Foreground = new SolidColorBrush(Colors.Black);
toast.FontSize = 20;
toast.Background = new SolidColorBrush(Colors.Yellow);
toast.Message = "Downloading starts";
toast.Show();
try
{
byte[] buffer = new byte[e.Result.Length];
//Store bytes in buffer, so it can be saved later on
await e.Result.ReadAsync(buffer, 0, buffer.Length);
using (IsolatedStorageFile file
IsolatedStorageFile.GetUserStoreForApplication())
{
//Create file
using (IsolatedStorageFileStream stream =
file.OpenFile(titleUrl+".mp3", FileMode.Create))
{
//Write content stream to file
await stream.WriteAsync(buffer, 0, buffer.Length);
}
// StorageFolder local =
Windows.Storage.ApplicationData.Current.LocalFolder;
//StorageFile storedFile = await local.GetFileAsync(titleUrl +
".mp3");
toast.Message = "Downloading complete";
toast.Show();
}
catch (Exception ex)
{
toast.Message = "We could not finish your download. Error ";
toast.Show();
}
Upvotes: 1
Views: 328
Reputation: 3166
It looks like you need to use the MediaLibraryExtensions.SaveSong
method (see https://msdn.microsoft.com/library/microsoft.xna.framework.media.PhoneExtensions.medialibraryextensions.savesong(v=xnagamestudio.42).aspx)
You'll need to add the ID_CAP_MEDIALIB_AUDIO
capability to your app, and get a URI for the file on the Isolated Storage to pass to that method.
There's some more information on Data for Windows Phone on MSDN.
Upvotes: 1