コード例 #1
0
        /// <summary>
        /// Fires when a message is recieved from the foreground app
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
        {
            foreach (string key in e.Data.Keys)
            {
                switch (key.ToLower())
                {
                case Constants.AppSuspended:
                    Debug.WriteLine("App suspending");     // App is suspended, you can save your task state at this point
                    foregroundAppState = ForegroundAppStatus.Suspended;
                    ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, Playlist.CurrentTrackName);
                    break;

                case Constants.AppResumed:
                    Debug.WriteLine("App resuming");     // App is resumed, now subscribe to message channel
                    foregroundAppState = ForegroundAppStatus.Active;
                    break;

                case Constants.StartPlayback:     //Foreground App process has signalled that it is ready for playback
                    Debug.WriteLine("Starting Playback");
                    StartPlayback();
                    break;

                case Constants.SkipNext:     // User has chosen to skip track from app context.
                    Debug.WriteLine("Skipping to next");
                    SkipToNext();
                    break;

                case Constants.SkipPrevious:     // User has chosen to skip track from app context.
                    Debug.WriteLine("Skipping to previous");
                    SkipToPrevious();
                    break;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Handles background task cancellation. Task cancellation happens due to :
        /// 1. Another Media app comes into foreground and starts playing music
        /// 2. Resource pressure. Your task is consuming more CPU and memory than allowed.
        /// In either case, save state so that if foreground app resumes it can know where to start.
        /// </summary>
        private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            // You get some time here to save your state before process and resources are reclaimed
            Debug.WriteLine("MyBackgroundAudioTask " + sender.Task.TaskId + " Cancel Requested...");
            try
            {
                //save state
                ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, Playlist.CurrentTrackName);
                ApplicationSettingsHelper.SaveSettingsValue(Constants.Position, BackgroundMediaPlayer.Current.Position.ToString());
                ApplicationSettingsHelper.SaveSettingsValue(Constants.BackgroundTaskState, Constants.BackgroundTaskCancelled);
                ApplicationSettingsHelper.SaveSettingsValue(Constants.AppState, Enum.GetName(typeof(ForegroundAppStatus), foregroundAppState));
                backgroundtaskrunning = false;
                //unsubscribe event handlers
                systemmediatransportcontrol.ButtonPressed   -= systemmediatransportcontrol_ButtonPressed;
                systemmediatransportcontrol.PropertyChanged -= systemmediatransportcontrol_PropertyChanged;
                Playlist.TrackChanged -= playList_TrackChanged;

                //clear objects task cancellation can happen uninterrupted
                playlistManager.ClearPlaylist();
                playlistManager = null;
                BackgroundMediaPlayer.Shutdown(); // shutdown media pipeline
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            deferral.Complete(); // signals task completion.
            Debug.WriteLine("MyBackgroundAudioTask Cancel complete...");
        }
コード例 #3
0
        /// <summary>
        /// Fires when playlist changes to a new track
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void playList_TrackChanged(MyPlaylist sender, object args)
        {
            string title;

            if (args is string)
            {
                title = args.ToString();
            }
            else
            {
                title = Playlist.CurrentTrackName;
            }


            UpdateUVCOnNewTrack(title);
            ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, sender.CurrentTrackName);

            if (foregroundAppState == ForegroundAppStatus.Active)
            {
                //Message channel that can be used to send messages to foreground
                ValueSet message = new ValueSet();
                message.Add(Constants.Trackchanged, title);
                BackgroundMediaPlayer.SendMessageToForeground(message);
            }
        }
コード例 #4
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...");

            //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();
        }
コード例 #5
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();
        }
コード例 #6
0
        /// <summary>
        /// Fires when a message is recieved from the foreground app
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
        {
            foreach (var one in e.Data)
            {
                string key = one.Key;
                switch (key.ToLower())
                {
                case Constants.AppSuspended:
                    Debug.WriteLine("App suspending");     // App is suspended, you can save your task state at this point
                    foregroundAppState = ForegroundAppStatus.Suspended;
                    ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, Playlist.CurrentTrackName);
                    break;

                case Constants.AppResumed:
                    Debug.WriteLine("App resuming");     // App is resumed, now subscribe to message channel
                    foregroundAppState = ForegroundAppStatus.Active;
                    break;

                case Constants.Trackat:     // User has chosen to skip track from app context.
                    foregroundAppState = ForegroundAppStatus.Active;
                    Debug.WriteLine("play to Trackat");
                    Trackat(one.Value.ToString());
                    break;

                case Constants.StartPlayback:     //Foreground App process has signalled that it is ready for playback
                    foregroundAppState = ForegroundAppStatus.Active;
                    Debug.WriteLine("Starting Playback");
                    StartPlayback();
                    break;

                case Constants.SkipNext:     // User has chosen to skip track from app context.
                    foregroundAppState = ForegroundAppStatus.Active;
                    Debug.WriteLine("Skipping to next");
                    SkipToNext();
                    break;

                case Constants.SkipPrevious:     // User has chosen to skip track from app context.
                    foregroundAppState = ForegroundAppStatus.Active;
                    Debug.WriteLine("Skipping to previous");
                    SkipToPrevious();
                    break;

                case Constants.conCurrentFile:
                    Debug.WriteLine("Play current File");
                    object value = string.Empty;
                    bool   rev   = e.Data.TryGetValue(Constants.conCurrentFile, out value);
                    if (rev)
                    {
                        string fileToken = value.ToString();
                        StartFile(fileToken);
                    }
                    break;

                case Constants.conPlaySingleFile:
                    object v  = string.Empty;
                    bool   bl = e.Data.TryGetValue(Constants.conPlaySingleFile, out v);
                    if (bl)
                    {
                        string fileToken = v.ToString();
                        StartSingleFile(fileToken);
                    }
                    break;

                case Constants.conPlayByIndex:
                    object vByindex = string.Empty;
                    bool   bplay    = e.Data.TryGetValue(Constants.conPlayByIndex, out vByindex);
                    if (bplay)
                    {
                        int index = Convert.ToInt32(vByindex);
                        StartPlayback(index);
                    }
                    break;
                }
            }
        }