0

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;
        }
    }
Jose Afonso
  • 117
  • 8
  • I'm confused about what you mean by "I couldn't make selecting any file from the list work". Why not put another list in XAML that shows all the files so that users could select which to play? – Roy Li - MSFT Nov 18 '21 at 02:15
  • I did it, Roy, and it's working. I just needed to use MediaPlaybackList's MoveTo() method. Thanks. – Jose Afonso Nov 18 '21 at 18:48
  • That's more confusing. If you have a list that shows all the media files in your XAML, then you should be able to get the Item that the user chooses in the `selesctionchanged event`. And once you've got the selected item, you should be able to know its index and call the `MediaPlaybackList.MoveTo() method`. So what blocks you from calling the `MediaPlaybackList.MoveTo() method`? – Roy Li - MSFT Nov 19 '21 at 02:19

0 Answers0