我有一个用于播放音频的 UWP 桌面应用程序。用户可以从包含几十个其他文件的列表中选择要播放的文件。这个列表被定义为一个 MediaPlaybackItem 列表,当做出选择时,我将 MediaPlayerElement 的 Source 属性设置为从列表中选择的 MediaPlaybackItem。问题是如果用户单击 NextTrack 或 PreviousTrack 按钮,事件不会触发。我尝试使用 MediaPlaybackList 作为 MediaPlayerElement 的源并解决了按钮问题,但是使用这种方法我无法从列表中选择任何文件。我无法设置 MediaPlayerElement 轨道。任何帮助都是最受欢迎的。谢谢。
<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;
}
}