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.
This commit is contained in:
ByteHamster 2023-02-02 00:06:53 +01:00 committed by GitHub
parent db5d47967a
commit 530165206b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 7 deletions

View File

@ -220,6 +220,24 @@ public class PlaybackService extends MediaBrowserServiceCompat {
PreferenceManager.getDefaultSharedPreferences(this) PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(prefListener); .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); ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver); mediaButtonIntent.setComponent(eventReceiver);
@ -243,13 +261,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
recreateMediaPlayer(); recreateMediaPlayer();
mediaSession.setActive(true); mediaSession.setActive(true);
castStateListener = new CastStateListener(this) {
@Override
public void onSessionStartedOrEnded() {
recreateMediaPlayer();
}
};
EventBus.getDefault().post(new PlaybackServiceEvent(PlaybackServiceEvent.Action.SERVICE_STARTED));
} }
void recreateMediaPlayer() { void recreateMediaPlayer() {
@ -290,6 +301,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefListener); PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefListener);
if (mediaSession != null) { if (mediaSession != null) {
mediaSession.release(); mediaSession.release();
mediaSession = null;
} }
unregisterReceiver(autoStateUpdated); unregisterReceiver(autoStateUpdated);
unregisterReceiver(headsetDisconnected); unregisterReceiver(headsetDisconnected);
@ -702,6 +714,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
mediaPlayer.playMediaObject(playable, stream, true, true); mediaPlayer.playMediaObject(playable, stream, true, true);
stateManager.validStartCommandWasReceived(); stateManager.validStartCommandWasReceived();
stateManager.startForeground(R.id.notification_playing, notificationBuilder.build());
recreateMediaSessionIfNeeded();
updateNotificationAndMediaSession(playable); updateNotificationAndMediaSession(playable);
addPlayableToQueue(playable); addPlayableToQueue(playable);
} }
@ -774,9 +788,11 @@ public class PlaybackService extends MediaBrowserServiceCompat {
case PLAYING: case PLAYING:
PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus()); PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus());
saveCurrentPosition(true, null, Playable.INVALID_TIME); saveCurrentPosition(true, null, Playable.INVALID_TIME);
recreateMediaSessionIfNeeded();
updateNotificationAndMediaSession(newInfo.playable); updateNotificationAndMediaSession(newInfo.playable);
setupPositionObserver(); setupPositionObserver();
stateManager.validStartCommandWasReceived(); stateManager.validStartCommandWasReceived();
stateManager.startForeground(R.id.notification_playing, notificationBuilder.build());
// set sleep timer if auto-enabled // set sleep timer if auto-enabled
if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING
&& SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { && SleepTimerPreferences.autoEnable() && !sleepTimerActive()) {