コード例 #1
0
        /// <summary>
        /// 后台任务的入口方法
        /// </summary>
        /// <param name="taskInstance">表示当前任务的实例</param>
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            //初始化后台消息处理的线程锁
            o = new object();

            //初始化系统音频控制器
            smtc = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
            smtc.ButtonPressed    += Smtc_ButtonPressed;
            smtc.PropertyChanged  += Smtc_PropertyChanged;
            smtc.IsEnabled         = true;
            smtc.IsNextEnabled     = true;
            smtc.IsPreviousEnabled = true;
            smtc.IsPlayEnabled     = true;
            smtc.IsPauseEnabled    = true;

            //获取前台APP状态
            var appState = BackgroundAudioSettingsHelper.GetValue(BackgroundAudioSettingsConstants.APP_STATE);

            if (appState == null)
            {
                foregroundAppState = AppState.Unknown;
            }
            else
            {
                foregroundAppState = EnumHelper.Parse <AppState>(appState.ToString());
            }

            //注册后台播放器的状态改变事件
            BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;

            //注册后台播放器发生错误时的事件
            BackgroundMediaPlayer.Current.MediaFailed += Current_MediaFailed;
            BackgroundMediaPlayer.Current.MediaEnded  += Current_MediaEnded;

            //注册后台播放器接收前台消息事件
            BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

            //如果前台APP未处于挂起状态,发送后台音频启动消息给前台
            if (foregroundAppState != AppState.Suspended)
            {
                MessageService.SendMessageToForeground(new BackgroundAudioTaskStartedMessage());
            }

            //保存后台音频任务的启动状态
            BackgroundAudioSettingsHelper.SetValue(BackgroundAudioSettingsConstants.BACKGROUND_TASK_STATE, BackgroundTaskState.Running.ToString());

            deferral = taskInstance.GetDeferral();

            backgroundTaskStarted.Set();

            //注册后台任务结束事件
            taskInstance.Task.Completed += Task_Completed;

            //注册后台任务取消事件
            taskInstance.Canceled += TaskInstance_Canceled;
        }
コード例 #2
0
        /// <summary>
        /// 开始播放音乐。
        /// 如果是第一次启动播放,直接播放;如果不是第一次启动播放,需要跳转到上次保存的进度继续播放。
        /// </summary>
        //private void StartPlayback()
        //{
        //    try
        //    {
        //        BackgroundMediaPlayer.Current.Source = playbackList;

        //        var currentTrackIndex = BackgroundAudioSettingsHelper.GetValue(BackgroundAudioSettingsConstants.TRACK_INDEX);
        //        var currentTrackPosition = BackgroundAudioSettingsHelper.GetValue(BackgroundAudioSettingsConstants.POSITION);

        //        if (!isFirstTimeToStart)
        //        {
        //            uint index;
        //            if (currentTrackIndex != null && uint.TryParse(currentTrackIndex.ToString(), out index))
        //            {
        //                currentIndex = (int)index;
        //                if (currentTrackPosition == null)
        //                {
        //                    playbackList.MoveTo(index);
        //                    BackgroundMediaPlayer.Current.Play();
        //                }
        //                else
        //                {
        //                    TypedEventHandler<MediaPlaybackList, CurrentMediaPlaybackItemChangedEventArgs> handler = null;
        //                    handler =
        //                        (MediaPlaybackList list, CurrentMediaPlaybackItemChangedEventArgs args) =>
        //                        {
        //                            try
        //                            {
        //                                if (args.NewItem.Equals(playbackList.Items[(int)index]))
        //                                {
        //                                    playbackList.CurrentItemChanged -= handler;

        //                                    var position = TimeSpan.Parse((string)currentTrackPosition);
        //                                    BackgroundMediaPlayer.Current.Position = position;

        //                                    BackgroundMediaPlayer.Current.Play();
        //                                }
        //                            }
        //                            catch
        //                            {

        //                            }
        //                        };

        //                    playbackList.CurrentItemChanged += handler;
        //                    playbackList.MoveTo(index);
        //                }
        //            }
        //            else
        //            {
        //                BackgroundMediaPlayer.Current.Play();
        //            }

        //        }
        //        else
        //        {
        //            BackgroundMediaPlayer.Current.Play();
        //            isFirstTimeToStart = false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.WriteLine(ex.ToString() + " " + ex.Message);
        //    }
        //}

        private void StartPlayback()
        {
            try
            {
                var currentTrackIndex = BackgroundAudioSettingsHelper.GetValue(BackgroundAudioSettingsConstants.TRACK_INDEX);
                var positionSetting   = BackgroundAudioSettingsHelper.GetValue(BackgroundAudioSettingsConstants.POSITION);
                if (!isFirstTimeToStart)
                {
                    int index;
                    if (currentTrackIndex != null && int.TryParse(currentTrackIndex.ToString(), out index))
                    {
                        if (positionSetting == null)
                        {
                            PlayIndex(index);
                        }
                        else
                        {
                            var position = TimeSpan.Parse((string)positionSetting);
                            PlayIndexWithPosition(index, position);
                        }
                    }
                    else
                    {
                        PlayIndex(0);
                    }
                }
                else
                {
                    PlayIndex(0);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }