標籤:style class blog code http tar
原文:Windows Phone 8.1 多媒體(3):音樂
Windows Phone 8.1 多媒體(1):相片
Windows Phone 8.1 多媒體(2):視頻
Windows Phone 8.1 多媒體(3):音樂
(1)前台
說是前台,其實音樂的播放都是利用系統背景 BackgroundMediaPlayer:
BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///djlang59_-_Drops_of_H2O_(_The_Filtered_Water_Treatment_).mp3"));BackgroundMediaPlayer.Current.Play();
可以添加音樂播放狀態改變事件:
BackgroundMediaPlayer.Current.CurrentStateChanged += MediaPlayerStateChanged;private async void MediaPlayerStateChanged(MediaPlayer sender, object args{ await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { switch( BackgroundMediaPlayer.Current.CurrentState ) { case MediaPlayerState.Stopped: AppBarBtnPause.IsEnabled = false; AppBarBtnStop.IsEnabled = false; break; case MediaPlayerState.Playing: AppBarBtnPause.IsEnabled = true; AppBarBtnStop.IsEnabled = true; BackgroundMediaPlayer.SendMessageToBackground(new ValueSet { {"Title", "Drops of H2O"}, {"Artist", "J.Lang"}, }); break; case MediaPlayerState.Paused: AppBarBtnPause.IsEnabled = false; break; } });}
其中 BackgroundMediaPlayer.SendMessageToBackground 方法可以向後台音樂播放程式發送訊息,可在背景程式中添加處理事件:
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground;private void BackgroundMediaPlayerOnMessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e){ systemmediatransportcontrol.DisplayUpdater.Type = MediaPlaybackType.Music; systemmediatransportcontrol.DisplayUpdater.MusicProperties.Title = e.Data["Title"].ToString(); systemmediatransportcontrol.DisplayUpdater.MusicProperties.Artist = e.Data["Artist"].ToString(); systemmediatransportcontrol.DisplayUpdater.Update();}
(2)後台
這裡說的後台是指 SystemMediaTransportControls,也就是按下音量鍵後頂部彈出的控制音樂播放的控制項。
1)建立 Windows Runtime Component 和繼承 IBackgroundTask 的後台類
A:Run 方法中可設定 SystemMediaTransportControls 的各個按鍵屬性以及事件的訂閱(比如下一曲按鈕可不可用,狀態變更事件等)
public void Run(IBackgroundTaskInstance taskInstance){ systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView(); systemmediatransportcontrol.ButtonPressed += SystemControlsButtonPressed; systemmediatransportcontrol.IsEnabled = true; systemmediatransportcontrol.IsPauseEnabled = true; systemmediatransportcontrol.IsStopEnabled = true; systemmediatransportcontrol.IsPlayEnabled = true; BackgroundMediaPlayer.Current.CurrentStateChanged -= BackgroundMediaPlayerCurrentStateChanged; BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayerOnMessageReceivedFromForeground; BackgroundMediaPlayer.Current.CurrentStateChanged += BackgroundMediaPlayerCurrentStateChanged; BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground; deferral = taskInstance.GetDeferral();}
B:事件的處理
private void BackgroundMediaPlayerCurrentStateChanged(MediaPlayer sender, object args){ if (sender.CurrentState == MediaPlayerState.Playing) { systemmediatransportcontrol.PlaybackStatus = MediaPlaybackStatus.Playing; } else if (sender.CurrentState == MediaPlayerState.Paused) { systemmediatransportcontrol.PlaybackStatus = MediaPlaybackStatus.Paused; }}private static void SystemControlsButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args){ switch (args.Button) { case SystemMediaTransportControlsButton.Play: BackgroundMediaPlayer.Current.Play(); break; case SystemMediaTransportControlsButton.Pause: BackgroundMediaPlayer.Current.Pause(); break; case SystemMediaTransportControlsButton.Stop: BackgroundMediaPlayer.Current.Pause(); BackgroundMediaPlayer.Current.Position = TimeSpan.FromSeconds(0); break; }}
2)前台應用添加上一步建立的背景工作