IT tutorials
 
Technology
 

Windows Phone 8 : AudioPlayerAgent Sample (part 2) - AudioPlayerAgent Virtual Methods

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

AudioPlayerAgent has the following three virtual methods that can be overridden in your implementation:

- OnPlayStateChanged

- OnUserAction

- OnError

The following sections explore each method in detail.

AudioPlayerAgent.OnPlayStateChanged

OnPlayStateChanged enables you to respond to changes in the playback state of an audio track. For example, it allows you to transition the play state of a track once it has completed downloading or to move to the next track when a track finishes, as shown in the following excerpt:

protected override void OnPlayStateChanged(
    BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
    switch (playState)
    {
        case PlayState.TrackReady:
            /* The track to play is set in the PlayTrack method. */
            player.Play();
            break;

        case PlayState.TrackEnded:
            PlayNextTrack(player);
            break;
    }

    NotifyComplete();
}

Table 1 describes the PlayState enum values that can be handled in the OnPlayStateChanged method.

TABLE 1. PlayState Enum

Image

Note

Whereas the OnPlayStateChanged method is used to respond to playback state changes, the OnError method is called when an error occurs during playback, and not OnPlayStateChanged.


AudioPlayerAgent.OnUserAction

The name of the OnUserAction method is somewhat misleading. The task of the OnUserAction method is to handle BackgroundAudioPlayer actions that occur in your foreground app.

Two actions cause the AudioPlayerAgent.OnUserAction method to be called. The first action occurs when the user presses the Universal Volume Control (UVC), and an onscreen menu is displayed allowing the user to play, pause, and skip forward and backward between tracks (see Figure 2). User actions do not directly affect the BackgroundAudioPlayer, but rather your AudioPlayerAgent is responsible for manipulating the player via the OnUserAction method.

Image

FIGURE 2 Tapping the onscreen menu background launches your app.

The second action occurs when your app is in the foreground and it calls one of the playback related methods on the BackgroundAudioPlayer.Instance object, such as Play. This type of action may or may not have been directed by the user.


Tip

The volume up and volume down buttons and the play/pause button of the UVC can be simulated in the Windows Phone emulator using the F9, F10, and F11 keys, respectively.

Although a function key simulates pressing the play/pause button, the play/pause button is not a required hardware button on Windows Phone devices and therefore may not be present on every device.


OnUserAction is passed a UserAction enum value that allows you to determine the behavior of the audio player agent. In some cases, the player can be put into action immediately. See the following excerpt:

protected override void OnUserAction(
    BackgroundAudioPlayer player, AudioTrack track, UserAction action,
    object param)
{
    /* User actions can be initiated from the main application
     * or from the UVC (Universal Volume Control). */
    switch (action)
    {

        case UserAction.Play:
            PlayTrack(player);
            break;

        case UserAction.Pause:
            player.Pause();
            break;

        case UserAction.SkipPrevious:
            PlayPreviousTrack(player);
            break;

        case UserAction.SkipNext:
            PlayNextTrack(player);
            break;

        case UserAction.Stop:
            player.Stop();
            break;
    }

    NotifyComplete();
}

Table 1 lists the UserAction enum values that can be handled in the OnUserAction method.

TABLE 1. UserAction Enum

Image

Note

The param object argument, of the OnUserAction method, is used only with the action argument UserAction.Seek, in which case the param argument is a TimeSpan that indicates the requested track position.


AudioPlayerAgent.OnError

OnError is called when an Exception is raised during playback of an audio track. The method is generally called when the agent encounters a connection error when downloading an audio file from a remote server and offers the opportunity to transition to a different track, one that is potentially stored locally if the error is due to an absence of network connectivity.

In the following excerpt you see that when an error is determined to be fatal, the Abort method is called, signaling to the OS that playback should be disabled. The method implementation is identical to the base class implementation and need not be implemented if you do not want to provide explicit error handling:

protected override void OnError(BackgroundAudioPlayer player,
                                AudioTrack track,
                                Exception error, bool isFatal)
{
    if (isFatal)
    {
        Abort();
    }
    else
    {
        NotifyComplete();
    }
}

 
Others
 
- 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
- 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)
 
 
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