Windows Phone 8.1 多媒體(3):音樂

來源:互聯網
上載者:User

標籤: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)前台應用添加上一步建立的背景工作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.