IT tutorials
 
Technology
 

Windows Phone 8 : AudioPlayerAgent Sample (part 1)

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

The custom audio player agent for this section includes a simple playlist, which includes both locally stored and remote audio files. The class is called BackgroundAudioPlayerAgent, and it is located in the WPUnleashed.BackgroundAudio project, within the BackgroundAudio solution in the downloadable sample code.

A static list of audio tracks defines the playlist, shown in the following excerpt:

static readonly List<AudioTrack> playList
    = new List<AudioTrack>
        {
            new AudioTrack(new Uri("AudioFiles/Audio01.wma",
                                    UriKind.Relative),
                              "Ringtone 1",              // title
                              "Windows Phone",           // artist
                              "Windows Phone Ringtones", // album
                              null),
            /* ... */

            /* A remote URI. */
            new AudioTrack(new Uri("http://example.com/track.mp3",
                                   UriKind.Absolute),
                              "Episode 29",
                              "Windows Phone Radio",
                              "Windows Phone Radio Podcast",
                              null)
        };


Tip

AudioPlayerAgents remain active during playback. This allows the creation of dynamic playlists. During playback, you could retrieve a playlist on a background thread from a remote server.


If providing playback of local media files, the files must be copied to isolated storage before they can be played by the background audio player. The IIsolatedStorageUtility is a custom interface that abstracts access to isolated storage. It is used by the BackgroundAudioPlayerAgent to detect whether the media files have already been written to isolated storage; if not, it is used to copy the sample files. The downloadable sample code contains all the code for the IIsolatedStorageUtility and its default implementation IsolatedStorageUtility. See the following excerpt:

internal static void CopyAudioToIsolatedStorage(
    IIsolatedStorageUtility isolatedStorageUtility)
{
    ArgumentValidator.AssertNotNull(
        isolatedStorageUtility, "isolatedStorageUtility");
    /* If a file exists, do not copy. */
    if (isolatedStorageUtility.FileExists("AudioFiles/Audio01.wma"))
    {
        return;
    }

    string[] files = new[] { "Audio01.wma", "Audio02.wma", "Audio03.wma" };

    var sourceToDestinations = from file in files
                        let path = "AudioFiles/" + file
                        select new KeyValuePair<string, string>(path, path);

    isolatedStorageUtility.CopyApplicationResourcesToIsolatedStorage(
                                                     sourceToDestinations);
}

The custom BackgroundAudioPlayerAgent keeps track of the current track number using a static int field. This is made possible because, unlike its ScheduledTaskAgent counterpart, the process for an AudioPlayerAgent is not terminated when NotifyComplete is called.

The track number field is used to select the correct track to play when the user requests the previous or next track, as shown:

void PlayNextTrack(BackgroundAudioPlayer player)
{
    if (++trackNumber >= playList.Count)
    {
        trackNumber = 0;
    }

    PlayTrack(player);
}

void PlayPreviousTrack(BackgroundAudioPlayer player)
{
    if (--trackNumber < 0)
    {
        trackNumber = playList.Count - 1;
    }

    PlayTrack(player);
}

A PlayTrack method is used to toggle the play state of the BackgroundAudioPlayer, as shown:

void PlayTrack(BackgroundAudioPlayer player)
{
    if (player.PlayerState == PlayState.Paused)
    {
        player.Play();
    }
    else
    {
        player.Track = playList[trackNumber];
    }
}

During playback, when the BackgroundAudioPlayer.Track property is set, the track does not begin immediate playback, but rather the BackgroundAudioPlayer is placed into a TrackReady state, and the AudioPlayerAgent.OnPlayStateChanged method is called. You see how this fits together in a moment.

 
Others
 
- 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
- Microsoft Exchange Server 2013 : Migration to modern public folders
- Microsoft Exchange Server 2013 : Migration to modern public folders
- Windows 8 : Using Secure Boot and SmartScreen Filter
- Windows 8 : Configuring network discovery and wireless security (part 2)
- Windows 8 : Configuring network discovery and wireless security (part 1) - Creating network discovery profiles
 
 
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