我已經通過msdn網站,stackoverflow等搜索了很多,但我無法在背景中播放一首歌曲。它停止時,改變框架,或退出應用程序返回到開始,它也隨機啓動時,它是在鎖屏或在其他地方..你能解釋如何正確做這個代碼請?對不起,代碼不好,但我已經在幾天前開始了。如何在Windows Phone 8.1中使用xaml和c#播放背景音樂?
這或多或少是我在微軟網站上發現的。還有沒有系統不會顯示任何控制
public sealed partial class listView : Page
{
SystemMediaTransportControls Player = SystemMediaTransportControls.GetForCurrentView();
SystemMediaTransportControls systemControls;
MediaElement musicPlayer = new MediaElement();
public listView()
{
this.InitializeComponent();
makeSongList();
// Per usare il tasto back
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
// Hook up app to system transport controls.
systemControls = SystemMediaTransportControls.GetForCurrentView();
systemControls.ButtonPressed += SystemControls_ButtonPressed;
// Register to handle the following system transpot control buttons.
systemControls.IsEnabled = true;
systemControls.IsPlayEnabled = true;
systemControls.IsPauseEnabled = true;
systemControls.IsNextEnabled = true;
systemControls.IsPreviousEnabled = true;
musicPlayer.AudioCategory = AudioCategory.BackgroundCapableMedia;
}
void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
{
// gestisce l'evento CurrentStateChanged di MediaElement e
// aggiorna la proprietà PlaybackStatus di SystemMediaTransportControls.
switch (musicPlayer.CurrentState)
{
case MediaElementState.Playing:
systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
break;
case MediaElementState.Paused:
systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case MediaElementState.Stopped:
systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
break;
case MediaElementState.Closed:
systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
break;
default:
break;
}
}
async private void UpdateSongInfo()
{
// Get the updater.
SystemMediaTransportControlsDisplayUpdater updater = systemControls.DisplayUpdater;
// Get the music file and pass it to CopyFromFileAsync to extract the metadata
// and thumbnail. StorageFile is defined in Windows.Storage
StorageFile musicFile =
await StorageFile.GetFileFromApplicationUriAsync(new Uri(currentPath));
await updater.CopyFromFileAsync(MediaPlaybackType.Music, musicFile);
// Update the system media transport controls
updater.Update();
}
void MusicPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
UpdateSongInfo();
}
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();
});
}
儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –
謝謝。更正:) – BADWOLF
@BADWOLF,但我怎麼能使用它,我不知道如何讓taskInstance運行它 –