简体   繁体   English

从外部服务器流式传输mp3文件

[英]Streaming mp3 file from external server

I am developing a Windows Phone 8 application. 我正在开发Windows Phone 8应用程序。 I need to stream a .mp3 sound file from a remote server in my application. 我需要从应用程序中的远程服务器流式传输.mp3声音文件。

I have tried to use MediaElement : 我尝试使用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. 我已仔细检查过,mp3文件的URI是否正确。

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 //获取MediaPlayer

You can play mp3 after media opened event is raised. 发生媒体打开事件后,您可以播放mp3。 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). 您可以在mediaOpened事件和您的方法(播放)中实现一些技巧。 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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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