Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SystemMediaTransportControls.ButtonPressed | buttonpressed event

Applies to Windows and Windows Phone

Occurs when a button is pressed on the SystemMediaTransportControls.

Syntax


function onButtonPressed(eventArgs) { /* Your code */ }
 
// addEventListener syntax
systemMediaTransportControls.addEventListener("buttonpressed", onButtonPressed);
systemMediaTransportControls.removeEventListener("buttonpressed", onButtonPressed);
 
- or -

systemMediaTransportControls.onbuttonpressed = onButtonPressed;


Event information

Delegate TypedEventHandler<SystemMediaTransportControls, SystemMediaTransportControlsButtonPressedEventArgs>

Remarks

Register a handler for the ButtonPressed event to integrate the system transport control button events with your app. Also, you must enable the individual transport control functions, such as SystemMediaTransportControls.IsPlayEnabled for handling the play event.

The Button property on the SystemMediaTransportControlsButtonPressedEventArgs indicates which button was pressed.

For more info see How to use the system media transport controls.

Examples

Here is some code that creates a ButtonPressed event handler and helper methods to play and pause the MediaElement. Notices that the calls to Play and Pause are marshaled to the UI thread through the CoreDispatcher using Dispatcher.RunAsync.


void SystemControls_ButtonPressed(SystemMediaTransportControls sender, 
    SystemMediaTransportControlsButtonPressedEventArgs args)
{
    switch (args.Button)
    {
        case SystemMediaTransportControlsButton.Play:
            PlayMedia();
            break;
        case SystemMediaTransportControlsButton.Pause:
            PauseMedia();
            break;
        default:
            break;
    }
}

async void PlayMedia()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        musicPlayer.Play();
    });
}

async void PauseMedia()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        musicPlayer.Pause();
    });
}


Here is the code that handles the CurrentStateChanged event handler and updates the SystemMediaTransportControls state.


void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
{
    switch (musicPlayer.CurrentState)
    {
        case MediaElementState.Playing:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
            break;
        case MediaElementState.Paused:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
            break;
        case MediaElementState.Stopped:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
            break;
        case MediaElementState.Closed:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
            break;
        default:
            break;
    }
}


Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.Media
Windows::Media [C++]

Metadata

Windows.winmd

See also

SystemMediaTransportControls

 

 

Show:
© 2014 Microsoft