Copying and Playing of an mp3 from a URL in Windows Phone 8

Posted by Vuyiswamb under Mobile Development on 11/9/2013 | Points: 10 | Views : 1085 | Status : [Member] [MVP] [Administrator] | Replies : 0
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

   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);
}
}

}

and i try to play it like this


    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;
}


Thanks

Thank you for posting at Dotnetfunda
[Administrator]



Responses

(No response found.)

Login to post response