Windows Phone 8.1 多媒体(3):音乐
2020-12-13 02:50
标签:style class blog code java http Windows Phone 8.1
多媒体(1):相片 Windows Phone 8.1
多媒体(2):视频 Windows Phone 8.1 多媒体(3):音乐 (1)前台 说是前台,其实音乐的播放都是利用系统后台的 BackgroundMediaPlayer: 可以添加音乐播放状态改变事件: 其中 BackgroundMediaPlayer.SendMessageToBackground
方法可以向后台音乐播放程序发送消息,可在后台程序中添加处理事件: (2)后台 这里说的后台是指
SystemMediaTransportControls,也就是按下音量键后顶部弹出的控制音乐播放的控件。 1)新建 Windows Runtime Component 和继承 IBackgroundTask
的后台类 A:Run 方法中可设置 SystemMediaTransportControls
的各个按键属性以及事件的订阅(比如下一曲按钮可不可用,状态更改事件等) B:事件的处理 2)前台应用添加上一步新建的后台任务 Windows Phone 8.1 多媒体(3):音乐,搜素材,soscw.com Windows Phone 8.1 多媒体(3):音乐 标签:style class blog code java http 原文地址:http://www.cnblogs.com/xiaoshi3003/p/3784174.html
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.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();
}
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();
}
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;
}
}