简体   繁体   中英

Streaming mp3 file from external server

I am developing a Windows Phone 8 application. I need to stream a .mp3 sound file from a remote server in my application.

I have tried to use MediaElement :

private MediaElement media;
// Constructor of class
            media = new MediaElement();
            media.Source = new Uri(string.Format("{0}b10en_US.mp3", mp3HostName), UriKind.Absolute);
            media.MediaFailed += media_MediaFailed;
            media.MediaEnded += media_MediaEnded;
            media.MediaOpened += media_MediaOpened;
            media.Loaded += media_Loaded;
            media.BufferingProgressChanged += media_BufferingProgressChanged;

// In a method I call the following
media.play();

However no sound gets played whatsoever. I added breakpoints to the mediaelement's events, but none gets fired.

I have double checked, the URI to the mp3 file is correct.

What am I doing wrong?

Try this :

        string url = "http://traffic.libsyn.com/slashfilmcast/Davidmichod.mp3";//your url link
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Microsoft.Xna.Framework.FrameworkDispatcher.Update();
            Song track = Song.FromUri("Sample Song", new Uri(url));
            MediaPlayer.Play(track);

        }

using Microsoft.Xna.Framework.Media; //for getting MediaPlayer

You can play mp3 after media opened event is raised. I think your method(in which you are calling play is called before media opened event is raised.

you can implement some hack in mediaOpened event and your method(play). like

private bool isMediaLoaded = false;
private bool isPlayCalled = false;
private void PlayMP3()
{
    if(isMediaLoaded)
       media.Play();
    else
       isPlayCalled = true;
}
void MediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
    isMediaLoaded = true;
    if(isPlayCalled)
        MediaElement1.Play();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM