IT tutorials
 
Technology
 

Windows Phone 8 : Audio Streaming Agents (part 2) - Using a MediaStreamSource to Play Back an Assembly Resource

3/1/2014 8:10:39 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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 1). 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 1. 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 2 demonstrates how to use a WebRequest to manually stream an audio file from the Web.

LISTING 2. 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.

 
Others
 
- Windows Phone 8 : Audio Streaming Agents (part 1)
- Windows Phone 8 : AudioPlayerAgent Sample (part 4) - Controlling Background Audio from Your Foreground App - Monitoring Playback Progress
- Windows Phone 8 : AudioPlayerAgent Sample (part 3) - Controlling Background Audio from Your Foreground App - MainPageViewModel
- Windows Phone 8 : AudioPlayerAgent Sample (part 2) - AudioPlayerAgent Virtual Methods
- Windows Phone 8 : AudioPlayerAgent Sample (part 1)
- Windows Phone 8 : Coordinating Background Audio Playback (part 2) - Representing Audio Files with the AudioTrack Class, Creating a Custom Audio Player Agent
- Windows Phone 8 : Coordinating Background Audio Playback (part 1) - Background Audio Player
- Microsoft Exchange Server 2013 : Site mailboxes (part 3) - The life cycle of site mailboxes, Site mailbox provisioning policy
- Microsoft Exchange Server 2013 : Site mailboxes (part 2) - How site mailboxes work - Synchronization between Exchange and SharePoint
- Microsoft Exchange Server 2013 : Site mailboxes (part 1) - How site mailboxes work
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us