Quantcast
Channel: VBForums - Mobile Development
Viewing all articles
Browse latest Browse all 96

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

$
0
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

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

and i try to play it like this


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

Thanks

Viewing all articles
Browse latest Browse all 96

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>