Windows Phone 8.1 多媒体(3):音乐_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Windows Phone 8.1 多媒体(3):音乐

Windows Phone 8.1 多媒体(3):音乐

 2014/6/12 23:21:56  消失3003  程序员俱乐部  我要评论(0)
  • 摘要:WindowsPhone8.1多媒体(1):相片WindowsPhone8.1多媒体(2):视频WindowsPhone8.1多媒体(3):音乐(1)前台说是前台,其实音乐的播放都是利用系统后台的BackgroundMediaPlayer:BackgroundMediaPlayer.Current.SetUriSource(newUri("ms-appx:///djlang59_-_Drops_of_H2O_(_The_Filtered_Water_Treatment_).mp3"))
  • 标签:Windows

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)前台应用添加上一步新建的后台任务

上一篇: Windows Phone 8.1 多媒体(1):相片 下一篇: 没有下一篇了!
发表评论
用户名: 匿名