/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> public void Run(IBackgroundTaskInstance taskInstance) { _appSettingsHelper = new AppSettingsHelper(); Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting..."); // InitializeAsync 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 = BackgroundMediaPlayer.Current.SystemMediaTransportControls; _systemmediatransportcontrol.ButtonPressed += systemmediatransportcontrol_ButtonPressed; // _systemmediatransportcontrol.PropertyChanged += systemmediatransportcontrol_PropertyChanged; _systemmediatransportcontrol.IsEnabled = true; _systemmediatransportcontrol.IsPauseEnabled = true; _systemmediatransportcontrol.IsPlayEnabled = true; _systemmediatransportcontrol.IsNextEnabled = true; _systemmediatransportcontrol.IsPreviousEnabled = true; //InitializeAsync message channel BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground; BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; //Send information to foreground that background task has been started if app is active if (ForegroundAppState == ForegroundAppStatus.Active) { var message = new ValueSet { { PlayerConstants.BackgroundTaskStarted, "" } }; BackgroundMediaPlayer.SendMessageToForeground(message); } _appSettingsHelper.Write(PlayerConstants.BackgroundTaskState, PlayerConstants.BackgroundTaskRunning); // This must be retrieved prior to subscribing to events below which use it _deferral = taskInstance.GetDeferral(); // Mark the background task as started to unblock SMTC Play operation (see related WaitOne on this signal) TaskStarted.Set(); taskInstance.Task.Completed += Taskcompleted; taskInstance.Canceled += OnCanceled; //Add handlers for MediaPlayer //Add handlers for playlist trackchanged QueueManager.TrackChanged += playList_TrackChanged; QueueManager.ErrorHandler += QueueManager_ErrorHandler; _backgroundtaskrunning = true; }