/// <summary>
        /// The Run method is the entry point of a background task.
        /// </summary>
        /// <param name="taskInstance"></param>
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting...");

            //SystemMediaTransportControls,是按下音量键后顶部弹出的控制音乐播放的控件。
            //设置 SystemMediaTransportControls 的各个按键属性以及事件的订阅
            systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
            systemmediatransportcontrol.ButtonPressed    += systemmediatransportcontrol_ButtonPressed;
            systemmediatransportcontrol.PropertyChanged  += systemmediatransportcontrol_PropertyChanged;
            systemmediatransportcontrol.IsEnabled         = true;
            systemmediatransportcontrol.IsPauseEnabled    = true;
            systemmediatransportcontrol.IsPlayEnabled     = true;
            systemmediatransportcontrol.IsNextEnabled     = true;
            systemmediatransportcontrol.IsPreviousEnabled = true;

            // 关联取消 后台任务处理完成
            taskInstance.Canceled       += new BackgroundTaskCanceledEventHandler(OnCanceled);
            taskInstance.Task.Completed += Taskcompleted;

            //读取应用程序状态
            var value = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.AppState);

            if (value == null)
            {
                foregroundAppState = ForegroundAppStatus.Unknown;
            }
            else
            {
                foregroundAppState = (ForegroundAppStatus)Enum.Parse(typeof(ForegroundAppStatus), value.ToString());
            }

            //添加在媒体播放器状态发生更改时处理函数
            BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;

            //Add handlers for playlist trackchanged
            //添加 后台播放列表变更时处理函数
            Playlist.TrackChanged  += playList_TrackChanged;
            Playlist.TrackComplete += playList_TrackComplete;

            //初始化消息通道 前台发往后台的信息
            BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

            //Send information to foreground that background task has been started if app is active
            //将信息发送到前景中的后台任务已经启动,如果应用程序被激活
            if (foregroundAppState != ForegroundAppStatus.Suspended)
            {
                ValueSet message = new ValueSet();
                message.Add(Constants.BackgroundTaskStarted, "");
                BackgroundMediaPlayer.SendMessageToForeground(message);
            }
            BackgroundTaskStarted.Set();
            backgroundtaskrunning = true;

            ApplicationSettingsHelper.SaveSettingsValue(Constants.BackgroundTaskState, Constants.BackgroundTaskRunning);
            object value11 = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.BackgroundTaskState);

            deferral = taskInstance.GetDeferral();
        }
Exemple #2
0
        /// <summary>
        /// The Run method is the entry point of a background task.
        /// </summary>
        /// <param name="taskInstance"></param>
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting...");
            // Initialize SMTC object to talk with UVC.
            //Note that, this is intended to run after app is paused and
            //hence all the logic must be written to run in background process
            systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
            systemmediatransportcontrol.ButtonPressed    += systemmediatransportcontrol_ButtonPressed;
            systemmediatransportcontrol.PropertyChanged  += systemmediatransportcontrol_PropertyChanged;
            systemmediatransportcontrol.IsEnabled         = true;
            systemmediatransportcontrol.IsPauseEnabled    = true;
            systemmediatransportcontrol.IsPlayEnabled     = true;
            systemmediatransportcontrol.IsNextEnabled     = true;
            systemmediatransportcontrol.IsPreviousEnabled = true;

            // Associate a cancellation and completed handlers with the background task.
            taskInstance.Canceled       += new BackgroundTaskCanceledEventHandler(OnCanceled);
            taskInstance.Task.Completed += Taskcompleted;

            var value = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.AppState);

            if (value == null)
            {
                foregroundAppState = ForegroundAppStatus.Unknown;
            }
            else
            {
                foregroundAppState = (ForegroundAppStatus)Enum.Parse(typeof(ForegroundAppStatus), value.ToString());
            }

            //Add handlers for MediaPlayer
            BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;

            //Add handlers for playlist trackchanged
            Playlist.TrackChanged += playList_TrackChanged;

            //Initialize message channel
            BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

            //Send information to foreground that background task has been started if app is active
            if (foregroundAppState != ForegroundAppStatus.Suspended)
            {
                ValueSet message = new ValueSet();
                message.Add(Constants.BackgroundTaskStarted, "");
                BackgroundMediaPlayer.SendMessageToForeground(message);
            }
            BackgroundTaskStarted.Set();
            backgroundtaskrunning = true;

            ApplicationSettingsHelper.SaveSettingsValue(Constants.BackgroundTaskState, Constants.BackgroundTaskRunning);
            deferral = taskInstance.GetDeferral();
        }
Exemple #3
0
        /// <summary>
        /// Start playlist and change UVC state
        /// </summary>

        private void StartPlayback()
        {
            try
            {
                if (Playlist.CurrentTrackName == string.Empty)
                {
                    //If the task was cancelled we would have saved the current track and its position. We will try playback from there
                    var currenttrackname     = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.CurrentTrack);
                    var currenttrackposition = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.Position);
                    if (currenttrackname != null)
                    {
                        if (currenttrackposition == null)
                        {
                            // play from start if we dont have position
                            Playlist.StartTrackAt((string)currenttrackname);
                        }
                        else
                        {
                            // play from exact position otherwise
                            Playlist.StartTrackAt((string)currenttrackname, TimeSpan.Parse((string)currenttrackposition));
                        }
                    }
                    else
                    {
                        //If we dont have anything, play from beginning of playlist.
                        Playlist.PlayAllTracks(); //start playback
                    }
                }
                else
                {
                    BackgroundMediaPlayer.Current.Play();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }