Good Day All
I am trying to stream an mp3 from a URL , So i first wanted to download the file first into the Isolated Storate or Local Storage and start to play the file. Below is the code i use to copy the File locally
and i try to play it like this
Thanks
I am trying to stream an mp3 from a URL , So i first wanted to download the file first into the Isolated Storate or Local Storage and start to play the file. Below is the code i use to copy the File locally
Code:
public static void CopyToIsolatedStorage(string _fileName)
{
List<string> MyList = new List<string>();
MyList.Add(_fileName);
foreach (var item in MyList)
{
using (IsolatedStorageFile localFile = IsolatedStorageFile.GetUserStoreForApplication())
{
Uri uri = new Uri(_fileName, UriKind.Absolute);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.BeginGetResponse((ar) =>
{
var response = request.EndGetResponse(ar);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
using (var stream = response.GetResponseStream())
{
var name = GetFileNameinURL(item);
if (localFile.FileExists(name))
{
localFile.DeleteFile(name);
}
using (IsolatedStorageFileStream fs = localFile.CreateFile(name))
{
stream.CopyTo(fs);
}
}
});
}, null);
}
}
}
Code:
private void btnPlay_Click(object sender, RoutedEventArgs e)
{ Button button = sender as Button;
//Copy file to Temp isolated Storate as Files can only play if they are in the Isolated Storage
GenericFunctions.CopyToIsolatedStorage(button.Tag.ToString()); //eg. url http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3
//<MediaElement Name="media" />
media.Source = new Uri(GenericFunctions.GetFileNameinURL(button.Tag.ToString()));
media.Play();
media.Volume = 1;
}