Search code examples
uwpmedia-player

How to get PreviousTrackButton/NextTrackButton click event of MediaPlayerElement?


I'm using UWP MediaPlayerElement.

When playing videos, use may click next track button to view next video.

How to get PreviousTrackButton/NextTrackButton Click event of MediaPlayerElement?


Solution

  • The button click events are handled directly by the MediaPlayerElement and the events are not directly surfaced. However, you can customize the Media Transport Control which surfaces them and add the events manually. You can refer to this documentation page for full instructions on customizing the default control behavior.

    In this case you will want to override the OnApplyTemplate method and subscribe to the button events there using GetTemplateChild method:

     protected override void OnApplyTemplate()
     {
        base.OnApplyTemplate();
    
        var previousButton = GetTemplateChild("TheButtonName") as Button;
        previousButton.Click += PreviousTrackClicked;
    }
    

    Where TheButtonName is the name of the specific button as you find it in the default style's template.