public void Run(IBackgroundTaskInstance taskInstance) { _settingsUtility = new SettingsUtility(); _foregroundMessenger = new ForegroundMessenger(); _smtcWrapper = new SmtcWrapper(BackgroundMediaPlayer.Current.SystemMediaTransportControls); _playerWrapper = new PlayerWrapper(_smtcWrapper, _foregroundMessenger, _settingsUtility); _settingsUtility.Write(ApplicationSettingsConstants.BackgroundTaskState, BackgroundTaskState.Running); // Send information to foreground that background task has been started if app is active if (_playerWrapper.ForegroundAppState != AppState.Suspended) MessageHelper.SendMessageToForeground(new BackgroundAudioTaskStartedMessage()); // 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(); // Associate a cancellation and completed handlers with the background task. taskInstance.Task.Completed += TaskCompleted; // event may raise immediately before continung thread excecution so must be at the end taskInstance.Canceled += OnCanceled; }
public void Run(IBackgroundTaskInstance taskInstance) { _settingsUtility = new SettingsUtility(); _foregroundMessenger = new ForegroundMessenger(); _smtcWrapper = new SmtcWrapper(BackgroundMediaPlayer.Current.SystemMediaTransportControls); _playerWrapper = new PlayerWrapper(_smtcWrapper, _foregroundMessenger, _settingsUtility); _settingsUtility.Write(ApplicationSettingsConstants.BackgroundTaskState, BackgroundTaskState.Running); // Send information to foreground that background task has been started if app is active if (_playerWrapper.ForegroundAppState != AppState.Suspended) { MessageHelper.SendMessageToForeground(new BackgroundAudioTaskStartedMessage()); } // 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(); // Associate a cancellation and completed handlers with the background task. taskInstance.Task.Completed += TaskCompleted; // event may raise immediately before continung thread excecution so must be at the end taskInstance.Canceled += OnCanceled; }
public void Dispose() { // save state _settingsUtility.Write(ApplicationSettingsConstants.QueueId, CurrentQueue.Id); _settingsUtility.Write(ApplicationSettingsConstants.Position, BackgroundMediaPlayer.Current.Position); _settingsUtility.Write(ApplicationSettingsConstants.BackgroundTaskState, BackgroundTaskState.Canceled); _settingsUtility.Write(ApplicationSettingsConstants.AppState, _foregroundMessenger); UnsubscribeFromMessenger(); UnsubscribeFromSmtc(); _smtcWrapper = null; _foregroundMessenger = null; }
public PlayerWrapper(SmtcWrapper smtcWrapper, ForegroundMessenger foregroundMessenger, ISettingsUtility settingsUtility) { _smtcWrapper = smtcWrapper; _foregroundMessenger = foregroundMessenger; _settingsUtility = settingsUtility; SubscribeToMessenger(); SubscribeToSmtc(); // Add handlers for MediaPlayer BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; // Read persisted state of foreground app ForegroundAppState = _settingsUtility.Read(ApplicationSettingsConstants.AppState, AppState.Unknown); }