Using a MediaStreamSource to Play Back an Assembly Resource

,

The built-in streaming and decoding system on the phone only supports playback of local audio files when they are located in isolated storage. This section looks at enabling the direct playback of audio files located within an app assembly.

The code for this section resides in the AssemblyAudioStreamingAgent class of the WPUnleashed.BackgroundAudio project in the downloadable sample code. AssemblyAudioStreamingAgent is a subclass of AudioStreamingAgent and overrides the base type’s OnBeginStreaming method (see Listing 34.2). The custom IsolatedStorageUtility is used to retrieve the byte stream for an audio file, whose location is specified by the AudioTrack.Tag property. An Mp3MediaStreamSource, from the ManagedMediaHelpers library, is created using the stream. The Mp3MediaStreamSource is then assigned to the AudioStreamer, which then relies on the Mp3MediaStreamSource to provide format-independent audio data.

LISTING 34.2. AssemblyAudioStreamingAgent.OnBeginStreaming Method


protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
{
    IsolatedStorageUtility utility = new IsolatedStorageUtility();
    Uri uri = new Uri(track.Tag, UriKind.Relative);
    Stream stream = utility.GetApplicationResourceStream(uri);
    Mp3MediaStreamSource mediaStreamSource
            = new Mp3MediaStreamSource(stream, stream.Length);
    streamer.SetSource(mediaStreamSource);

    /* Not to be called. */
    //NotifyComplete();
}



Note

Do not call NotifyComplete within the OnBeginStreaming method. Doing so causes the process running the AudioStreamingAgent to be terminated, which halts playback of the file.


Having the capability to provide your own custom streaming means that you can support other third-party services or retrieve files from, for example, a WCF service, which is not supported natively by the OS.

Listing 34.3 demonstrates how to use a WebRequest to manually stream an audio file from the Web.

LISTING 34.3. OnBeginStreaming Method with WebRequest


protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
{
    HttpWebRequest request
             = WebRequest.CreateHttp("http://localhost:4864/Audio.mp3");
    request.AllowReadStreamBuffering = true;
    request.BeginGetResponse(asyncResult =>
        {
            HttpWebResponse response
                = request.EndGetResponse(asyncResult) as HttpWebResponse;
            if (response != null)
            {
                Stream stream = response.GetResponseStream();
                mediaStreamSource = new Mp3MediaStreamSource(
                                            stream, response.ContentLength);
                streamer.SetSource(mediaStreamSource);
            }
    }, null);
}


Although not something you will need every day, audio streaming agents provide the means to overcome any limitations imposed by the built-in streaming and decoding system on the phone.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.8.127