void HandleSmtcButton(MediaPlayerManager mediaPlayerManager, SystemMediaTransportControlsButton button) { Debug.WriteLine("BackgroundAudioRun.HandleSmtcButton() " + _id + " button " + button); try { switch (button) { case SystemMediaTransportControlsButton.Play: mediaPlayerManager.Play(); break; case SystemMediaTransportControlsButton.Pause: mediaPlayerManager.Pause(); break; case SystemMediaTransportControlsButton.Next: _systemMediaTransportControls.PlaybackStatus = MediaPlaybackStatus.Changing; mediaPlayerManager.Next(); break; case SystemMediaTransportControlsButton.Previous: _systemMediaTransportControls.PlaybackStatus = MediaPlaybackStatus.Changing; mediaPlayerManager.Previous(); break; case SystemMediaTransportControlsButton.Stop: mediaPlayerManager.Stop(); break; } } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.HandleSmtcButton() failed " + ex.ExtendedMessage()); } }
void HandleSmtcButton(MediaPlayerManager mediaPlayerManager, SystemMediaTransportControlsButton button) { Debug.WriteLine("BackgroundAudioRun.HandleSmtcButton() " + _id + " button " + button); try { switch (button) { case SystemMediaTransportControlsButton.Play: mediaPlayerManager.Play(); break; case SystemMediaTransportControlsButton.Pause: mediaPlayerManager.Pause(); break; case SystemMediaTransportControlsButton.Next: _systemMediaTransportControls.PlaybackStatus = MediaPlaybackStatus.Changing; mediaPlayerManager.Next(); break; case SystemMediaTransportControlsButton.Previous: _systemMediaTransportControls.PlaybackStatus = MediaPlaybackStatus.Changing; mediaPlayerManager.Previous(); break; case SystemMediaTransportControlsButton.Stop: mediaPlayerManager.Stop(); break; } } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.HandleSmtcButton() failed " + ex.ExtendedMessage()); } }
public async Task ExecuteAsync() { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync()"); try { _systemMediaTransportControls = SystemMediaTransportControls.GetForCurrentView(); var smtc = _systemMediaTransportControls; var isOk = false; BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground; var mediaPlayer = BackgroundMediaPlayer.Current; _metadataHandler = new MetadataHandler(_systemMediaTransportControls, _foregroundNotifier, () => mediaPlayer.Position, position => UpdateMediaPlayerEvents(mediaPlayer, position), _cancellationTokenSource.Token); try { mediaPlayer.CurrentStateChanged += OnCurrentStateChanged; mediaPlayer.PlaybackMediaMarkerReached += OnPlaybackMediaMarkerReached; BackgroundSettings.SetBackgroundId(_id); isOk = true; } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() initialization failed: " + ex.ExtendedMessage()); } MediaPlayerManager mediaPlayerManager = null; if (isOk) { try { mediaPlayerManager = new MediaPlayerManager(mediaPlayer, _metadataHandler, _cancellationTokenSource.Token); mediaPlayerManager.TrackChanged += MediaPlayerManagerOnTrackChanged; mediaPlayerManager.Failed += MediaPlayerManagerOnFailed; mediaPlayerManager.Ended += MediaPlayerManagerOnEnded; _mediaPlayerManager = mediaPlayerManager; smtc.ButtonPressed += SystemMediaTransportControlsOnButtonPressed; smtc.PropertyChanged += SystemMediaTransportControlsOnPropertyChanged; smtc.IsEnabled = true; smtc.IsPauseEnabled = true; smtc.IsPlayEnabled = true; smtc.IsNextEnabled = true; smtc.IsPreviousEnabled = true; SyncNotification(); Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() running"); await _completionSource.Task.ConfigureAwait(false); } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() playback failed: " + ex.ExtendedMessage()); } } Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() done running"); BackgroundSettings.RemoveBackgroundId(_id); try { _timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); } catch (Exception) { // Guard against race with cleanup } _mediaPlayerManager = null; smtc.PropertyChanged -= SystemMediaTransportControlsOnPropertyChanged; smtc.ButtonPressed -= SystemMediaTransportControlsOnButtonPressed; if (null != mediaPlayerManager) { mediaPlayerManager.TrackChanged -= MediaPlayerManagerOnTrackChanged; mediaPlayerManager.Failed -= MediaPlayerManagerOnFailed; mediaPlayerManager.Ended -= MediaPlayerManagerOnEnded; await mediaPlayerManager.CloseAsync().ConfigureAwait(false); mediaPlayerManager.Dispose(); } if (_appId.HasValue) { _foregroundNotifier.Notify(BackgroundNotificationType.Stop); } mediaPlayer.CurrentStateChanged -= OnCurrentStateChanged; mediaPlayer.PlaybackMediaMarkerReached -= OnPlaybackMediaMarkerReached; BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayerOnMessageReceivedFromForeground; } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() failed: " + ex.ExtendedMessage()); } finally { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() completed"); } }
public async Task ExecuteAsync() { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync()"); try { _systemMediaTransportControls = SystemMediaTransportControls.GetForCurrentView(); var smtc = _systemMediaTransportControls; var isOk = false; BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground; var mediaPlayer = BackgroundMediaPlayer.Current; _metadataHandler = new MetadataHandler(_systemMediaTransportControls, _foregroundNotifier, () => mediaPlayer.Position, position => UpdateMediaPlayerEvents(mediaPlayer, position), _cancellationTokenSource.Token); try { mediaPlayer.CurrentStateChanged += OnCurrentStateChanged; mediaPlayer.PlaybackMediaMarkerReached += OnPlaybackMediaMarkerReached; BackgroundSettings.SetBackgroundId(_id); isOk = true; } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() initialization failed: " + ex.ExtendedMessage()); } MediaPlayerManager mediaPlayerManager = null; if (isOk) { try { mediaPlayerManager = new MediaPlayerManager(mediaPlayer, _metadataHandler, _cancellationTokenSource.Token); mediaPlayerManager.TrackChanged += MediaPlayerManagerOnTrackChanged; mediaPlayerManager.Failed += MediaPlayerManagerOnFailed; mediaPlayerManager.Ended += MediaPlayerManagerOnEnded; _mediaPlayerManager = mediaPlayerManager; smtc.ButtonPressed += SystemMediaTransportControlsOnButtonPressed; smtc.PropertyChanged += SystemMediaTransportControlsOnPropertyChanged; smtc.IsEnabled = true; smtc.IsPauseEnabled = true; smtc.IsPlayEnabled = true; smtc.IsNextEnabled = true; smtc.IsPreviousEnabled = true; SyncNotification(); Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() running"); await _completionSource.Task.ConfigureAwait(false); } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() playback failed: " + ex.ExtendedMessage()); } } Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() done running"); BackgroundSettings.RemoveBackgroundId(_id); try { _timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); } catch (Exception) { // Guard against race with cleanup } _mediaPlayerManager = null; smtc.PropertyChanged -= SystemMediaTransportControlsOnPropertyChanged; smtc.ButtonPressed -= SystemMediaTransportControlsOnButtonPressed; if (null != mediaPlayerManager) { mediaPlayerManager.TrackChanged -= MediaPlayerManagerOnTrackChanged; mediaPlayerManager.Failed -= MediaPlayerManagerOnFailed; mediaPlayerManager.Ended -= MediaPlayerManagerOnEnded; await mediaPlayerManager.CloseAsync().ConfigureAwait(false); mediaPlayerManager.Dispose(); } if (_appId.HasValue) _foregroundNotifier.Notify(BackgroundNotificationType.Stop); mediaPlayer.CurrentStateChanged -= OnCurrentStateChanged; mediaPlayer.PlaybackMediaMarkerReached -= OnPlaybackMediaMarkerReached; BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayerOnMessageReceivedFromForeground; } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() failed: " + ex.ExtendedMessage()); } finally { Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() completed"); } }