From 103cfee719d76effe9439dbf9f62638edba06644 Mon Sep 17 00:00:00 2001 From: nt4f04uNd Date: Tue, 12 Oct 2021 18:16:01 +0300 Subject: [PATCH] Fix service destroyed with activity --- .../ryanheise/audioservice/AudioService.java | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java index 075c0ee7..144bd4cb 100644 --- a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java +++ b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java @@ -285,6 +285,28 @@ public int onStartCommand(final Intent intent, int flags, int startId) { return START_NOT_STICKY; } + /** + * Restarts the foreground service from the idle state. + * + * The opposite of this method is {@link #stop()}. + */ + public void restart() { + ContextCompat.startForegroundService(this, new Intent(AudioService.this, AudioService.class)); + activateMediaSession(); + mediaSession.setSessionActivity(contentIntent); + } + + /** + * Tries to stop the service. + * + * The service will not be stopped, if it's in foreground state, + * or there are active {@link MediaBrowserCompat.ConnectionCallback}. + * + * If there are callbacks, the service will be destroyed as soon as they disconnect + * (not taking to account the foreground state). + * To prevent this, {@link #restart()} should be called, which is the + * opposite of this method. + */ public void stop() { deactivateMediaSession(); stopSelf(); @@ -409,7 +431,11 @@ else if (errorMessage != null) if (oldProcessingState != AudioProcessingState.idle && processingState == AudioProcessingState.idle) { // TODO: Handle completed state as well? stop(); - } else if (processingState != AudioProcessingState.idle && notificationChanged) { + } else if (oldProcessingState == AudioProcessingState.idle && processingState != AudioProcessingState.idle) { + restart(); + } + + if (processingState != AudioProcessingState.idle && notificationChanged) { updateNotification(); } } @@ -546,31 +572,18 @@ private void updateNotification() { } private void enterPlayingState() { - ContextCompat.startForegroundService(this, new Intent(AudioService.this, AudioService.class)); - if (!mediaSession.isActive()) - mediaSession.setActive(true); - acquireWakeLock(); - mediaSession.setSessionActivity(contentIntent); - internalStartForeground(); + startForeground(NOTIFICATION_ID, buildNotification()); + notificationCreated = true; } private void exitPlayingState() { if (config.androidStopForegroundOnPause) { - exitForegroundState(); + stopForeground(false); + releaseWakeLock(); } } - private void exitForegroundState() { - stopForeground(false); - releaseWakeLock(); - } - - private void internalStartForeground() { - startForeground(NOTIFICATION_ID, buildNotification()); - notificationCreated = true; - } - private void acquireWakeLock() { if (!wakeLock.isHeld()) wakeLock.acquire(); @@ -714,32 +727,28 @@ public void onRemoveQueueItem(MediaDescriptionCompat description) { @Override public void onPrepare() { if (listener == null) return; - if (!mediaSession.isActive()) - mediaSession.setActive(true); + activateMediaSession(); listener.onPrepare(); } @Override public void onPrepareFromMediaId(String mediaId, Bundle extras) { if (listener == null) return; - if (!mediaSession.isActive()) - mediaSession.setActive(true); + activateMediaSession(); listener.onPrepareFromMediaId(mediaId, extras); } @Override public void onPrepareFromSearch(String query, Bundle extras) { if (listener == null) return; - if (!mediaSession.isActive()) - mediaSession.setActive(true); + activateMediaSession(); listener.onPrepareFromSearch(query, extras); } @Override public void onPrepareFromUri(Uri uri, Bundle extras) { if (listener == null) return; - if (!mediaSession.isActive()) - mediaSession.setActive(true); + activateMediaSession(); listener.onPrepareFromUri(uri, extras); }