我阅读了this关于"如何在后台播放音频的文章(XAML)"并且工作,我很好地播放了我的mp3文件,但如果我尝试从应用程序音乐停止,我想"背景音频"即使应用程序没有关注屏幕也要播放!
XAML
<Grid>
<MediaElement x:Name="musicPlayer"
Source="Assets/VIGEVANO.mp3"
AudioCategory="BackgroundCapableMedia"
CurrentStateChanged="MusicPlayer_CurrentStateChanged" />
</Grid>
CS
SystemMediaTransportControls systemControls;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
// Hook up app to system transport controls.
systemControls = SystemMediaTransportControls.GetForCurrentView();
systemControls.ButtonPressed += SystemControls_ButtonPressed;
// Register to handle the following system transpot control buttons.
systemControls.IsPlayEnabled = true;
systemControls.IsPauseEnabled = true;
}
private 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();
});
}
我想念的是什么?