简体   繁体   中英

How do I stream an MP3 over HTTP in the background?

There are many examples on how to use the Background Audio Agent but very few show how to use the Background Audio Streaming Agent and the ones that I found don't show streaming mp3 but instead create a pretend stream.

When I create a new Windows Phone Audio Streaming Agent project, it gives me:

public class AudioTrackStreamer : AudioStreamingAgent
{
    /// <summary>
    /// Called when a new track requires audio decoding
    /// (typically because it is about to start playing)
    /// </summary>
    /// <param name="track">
    /// The track that needs audio streaming
    /// </param>
    /// <param name="streamer">
    /// The AudioStreamer object to which a MediaStreamSource should be
    /// attached to commence playback
    /// </param>
    /// <remarks>
    /// To invoke this method for a track set the Source parameter of the AudioTrack to null
    /// before setting  into the Track property of the BackgroundAudioPlayer instance
    /// property set to true;
    /// otherwise it is assumed that the system will perform all streaming
    /// and decoding
    /// </remarks>
    protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
    {
        //TODO: Set the SetSource property of streamer to a MSS source

        NotifyComplete();
    }

    /// <summary>
    /// Called when the agent request is getting cancelled
    /// The call to base.OnCancel() is necessary to release the background streaming resources
    /// </summary>
    protected override void OnCancel()
    {
        base.OnCancel();
    }
}

How do I give it a MP3 URL like http://relay.radioreference.com:80/346246215 and have it stream it in the background? Also do I put BackgroundAudioPlayer.Instance.Play(); to play it and that's it?

yes, that is enough No need of streamer, if you set the URL to the background agent and call the function BackgroundAudioPlayer.Instance.Play(); the background agent automatically streams the media

If you want to play streaming audio in a format/codec which is not natively supported by the phone you must do it with an AudioStreamingAgent . If it is a supported codec, you can use an AudioPlayerAgent (see sample here ).

Using an AudioStreamingAgent is a nontrivial task and requires a deep understanding of the codec you need to play so you can convert it to something the phone understands. I know if one person who did this, for a H.264 stream, and it took a long time and much hair pulling to get it working. And before anyone asks: No, they are not able to share code from that project.

If you really must go down this route, the ManagedMediaHelpers (previously here ) are a good place to start, but yes, they don't cover all codecs and this is, potentially, very complicated and not something well documented on the web.

You have an good example of AudioStreamingAgent on Github: https://github.com/loarabia/ManagedMediaHelpers .

I already tried it but only works when finding an ID3 header on the stream.

You can try my solution just little customize from background agent to set new track from mainPage.

SetTrack from foreground

You can also save the trackList to Isostorage and read the from background agent to 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