diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2023-02-02 00:06:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-02 00:06:53 +0100 |
commit | 530165206b9dd9155b00c0ba418a8bdcfcd51c69 (patch) | |
tree | e8f14d07b3b5039b100bae57547fb85fbcdc3471 /core/src/main | |
parent | db5d47967a6de37604ffa938d4100dcf7d28b917 (diff) | |
download | AntennaPod-530165206b9dd9155b00c0ba418a8bdcfcd51c69.zip |
Fix when playback is started from MediaSessionCompat.Callback (#6295)
In that case:
- The service does not go through onStartCommand, so it does not go to foreground state.
- The media session is already destroyed.
Now, create a new media session and definitely start foreground service when something is playing.
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 4fb7bcf2a..434aa77e7 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -220,6 +220,24 @@ public class PlaybackService extends MediaBrowserServiceCompat { PreferenceManager.getDefaultSharedPreferences(this) .registerOnSharedPreferenceChangeListener(prefListener); + recreateMediaSessionIfNeeded(); + castStateListener = new CastStateListener(this) { + @Override + public void onSessionStartedOrEnded() { + recreateMediaPlayer(); + } + }; + EventBus.getDefault().post(new PlaybackServiceEvent(PlaybackServiceEvent.Action.SERVICE_STARTED)); + } + + void recreateMediaSessionIfNeeded() { + if (mediaSession != null) { + // Media session was not destroyed, so we can re-use it. + if (!mediaSession.isActive()) { + mediaSession.setActive(true); + } + return; + } ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(eventReceiver); @@ -243,13 +261,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { recreateMediaPlayer(); mediaSession.setActive(true); - castStateListener = new CastStateListener(this) { - @Override - public void onSessionStartedOrEnded() { - recreateMediaPlayer(); - } - }; - EventBus.getDefault().post(new PlaybackServiceEvent(PlaybackServiceEvent.Action.SERVICE_STARTED)); } void recreateMediaPlayer() { @@ -290,6 +301,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefListener); if (mediaSession != null) { mediaSession.release(); + mediaSession = null; } unregisterReceiver(autoStateUpdated); unregisterReceiver(headsetDisconnected); @@ -702,6 +714,8 @@ public class PlaybackService extends MediaBrowserServiceCompat { mediaPlayer.playMediaObject(playable, stream, true, true); stateManager.validStartCommandWasReceived(); + stateManager.startForeground(R.id.notification_playing, notificationBuilder.build()); + recreateMediaSessionIfNeeded(); updateNotificationAndMediaSession(playable); addPlayableToQueue(playable); } @@ -774,9 +788,11 @@ public class PlaybackService extends MediaBrowserServiceCompat { case PLAYING: PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus()); saveCurrentPosition(true, null, Playable.INVALID_TIME); + recreateMediaSessionIfNeeded(); updateNotificationAndMediaSession(newInfo.playable); setupPositionObserver(); stateManager.validStartCommandWasReceived(); + stateManager.startForeground(R.id.notification_playing, notificationBuilder.build()); // set sleep timer if auto-enabled if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING && SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { |