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:
parent
db5d47967a
commit
530165206b
|
@ -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()) {
|
||||
|
|
Loading…
Reference in New Issue