Updating notification play button in onDestroy

This commit is contained in:
ByteHamster 2019-10-29 22:37:58 +01:00
parent eb5514c764
commit 7ae5424479
2 changed files with 28 additions and 8 deletions

View File

@ -326,6 +326,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
Log.d(TAG, "Service is about to be destroyed"); Log.d(TAG, "Service is about to be destroyed");
if (notificationBuilder.getPlayerStatus() == PlayerStatus.PLAYING) {
notificationBuilder.setPlayerStatus(PlayerStatus.STOPPED);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
stateManager.stopForeground(!UserPreferences.isPersistNotify()); stateManager.stopForeground(!UserPreferences.isPersistNotify());
isRunning = false; isRunning = false;
currentMediaType = MediaType.UNKNOWN; currentMediaType = MediaType.UNKNOWN;
@ -1197,7 +1203,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
} }
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus(); PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting); notificationBuilder.setPlayable(playable);
notificationBuilder.setMediaSessionToken(mediaSession.getSessionToken());
notificationBuilder.setPlayerStatus(playerStatus);
notificationBuilder.setCasting(isCasting);
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed()); notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
Log.d(TAG, "setupNotification: startForeground" + playerStatus); Log.d(TAG, "setupNotification: startForeground" + playerStatus);

View File

@ -46,16 +46,11 @@ public class PlaybackServiceNotificationBuilder {
this.context = context; this.context = context;
} }
public void setMetadata(Playable playable, MediaSessionCompat.Token mediaSessionToken, public void setPlayable(Playable playable) {
PlayerStatus playerStatus, boolean isCasting) {
if (playable != this.playable) { if (playable != this.playable) {
clearCache(); clearCache();
} }
this.playable = playable; this.playable = playable;
this.mediaSessionToken = mediaSessionToken;
this.playerStatus = playerStatus;
this.isCasting = isCasting;
} }
private void clearCache() { private void clearCache() {
@ -63,7 +58,7 @@ public class PlaybackServiceNotificationBuilder {
this.position = null; this.position = null;
} }
public void updatePosition(int position,float speed) { public void updatePosition(int position, float speed) {
TimeSpeedConverter converter = new TimeSpeedConverter(speed); TimeSpeedConverter converter = new TimeSpeedConverter(speed);
this.position = Converter.getDurationStringLong(converter.convert(position)); this.position = Converter.getDurationStringLong(converter.convert(position));
} }
@ -239,4 +234,20 @@ public class PlaybackServiceNotificationBuilder {
return PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
} }
public void setMediaSessionToken(MediaSessionCompat.Token mediaSessionToken) {
this.mediaSessionToken = mediaSessionToken;
}
public void setPlayerStatus(PlayerStatus playerStatus) {
this.playerStatus = playerStatus;
}
public void setCasting(boolean casting) {
isCasting = casting;
}
public PlayerStatus getPlayerStatus() {
return playerStatus;
}
} }