码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-06-13 17:57:46      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

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();

可以添加音乐播放状态改变事件:

bubuko.com,布布扣
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;
        }
    });
}
bubuko.com,布布扣

其中 BackgroundMediaPlayer.SendMessageToBackground 方法可以向后台音乐播放程序发送消息,可在后台程序中添加处理事件:

bubuko.com,布布扣
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();
}
bubuko.com,布布扣

 

(2)后台

这里说的后台是指 SystemMediaTransportControls,也就是按下音量键后顶部弹出的控制音乐播放的控件。

1)新建 Windows Runtime Component 和继承 IBackgroundTask 的后台类

A:Run 方法中可设置 SystemMediaTransportControls 的各个按键属性以及事件的订阅(比如下一曲按钮可不可用,状态更改事件等)

bubuko.com,布布扣
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();
}
bubuko.com,布布扣

B:事件的处理

bubuko.com,布布扣
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;
    }
}
bubuko.com,布布扣

 

2)前台应用添加上一步新建的后台任务

bubuko.com,布布扣

Windows Phone 8.1 多媒体(3):音乐,布布扣,bubuko.com

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

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/xiaoshi3003/p/3784174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!