Jose Afonso
Jose Afonso

Reputation: 117

How do I get the NextTrack button click on a MediaPlayerElement that has only one track?

I have a UWP Desktop application for playing audio. The user can select the file to be played from a list with dozens of other files. This list is defined as a MediaPlaybackItem List and when the selection is made I set the Source property of the MediaPlayerElement to the MediaPlaybackItem selected from the list. The problem is that if the user clicks the NextTrack or PreviousTrack button, the event does not fire. I tried using a MediaPlaybackList as the source of the MediaPlayerElement and that solves the button problem, but with that approach I couldn't make selecting any file from the list work. I couldn't set the MediaPlayerElement track. Any help is most welcome. Thanks.

<MediaPlayerElement x:Name="mediaPlayerElement" 
                                AutoPlay="False" 
                                HorizontalAlignment="Center" VerticalAlignment="Top"
                                Margin="0,0,0,100"
                                AreTransportControlsEnabled="True" >
                                <MediaPlayerElement.TransportControls>
                                    <MediaTransportControls 
                                             IsSkipBackwardEnabled="False"
                                             IsSkipBackwardButtonVisible="False"
                                             IsSkipForwardEnabled="False"
                                             IsSkipForwardButtonVisible="False"
                                             IsFastForwardButtonVisible="True"
                                             IsFastForwardEnabled="True"
                                             IsFastRewindButtonVisible="True"
                                             IsFastRewindEnabled="True" 
                                            IsFullWindowButtonVisible="False"
                                            IsNextTrackButtonVisible="True"
                                            IsPreviousTrackButtonVisible="True"
                                            IsZoomButtonVisible="False"/>
                                </MediaPlayerElement.TransportControls>
                            </MediaPlayerElement>

MediaPlaybackList mediaPlaybackList = new MediaPlaybackList();
List<MediaPlaybackItem> mediaPlaybackItems = new List<MediaPlaybackItem>();

mediaPlayerElement.MediaPlayer.SystemMediaTransportControls.ButtonPressed += SystemMediaTransportControls_ButtonPressed;

 private async void SystemMediaTransportControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Play:
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                        
                    });
                break;
            case SystemMediaTransportControlsButton.Pause:
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                       
                    });
                break;
            case SystemMediaTransportControlsButton.Previous:
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                        //This event is not fired when the MediaPlayerElement source is a MediaPlaybackItem. 
                    });
                break;
            case SystemMediaTransportControlsButton.Next:
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () =>
                    {
                        //This event is not fired when the MediaPlayerElement source is a MediaPlaybackItem. 
                    });
                break;
            case SystemMediaTransportControlsButton.Stop:
                break;
            default:
                break;
        }
    }

Upvotes: 0

Views: 61

Answers (0)

Related Questions