From 7db31317dab798be4e6dd5591aaa654438f6ac2f Mon Sep 17 00:00:00 2001 From: Ludevv Date: Wed, 10 Jun 2026 11:14:25 +0200 Subject: [PATCH] Guard startForeground calls with try/catch and add isForeground flag --- .../exoplayer/VideoPlaybackService.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt b/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt index e1c4457a7f..c83e294723 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/VideoPlaybackService.kt @@ -28,6 +28,7 @@ class VideoPlaybackService : MediaSessionService() { private var mediaSessionsList = mutableMapOf() private var binder = PlaybackServiceBinder(this) private var sourceActivity: Class? = null + private var isForeground = false // Controls for Android 13+ - see buildNotification function private val commandSeekForward = SessionCommand(COMMAND.SEEK_FORWARD.stringValue, Bundle.EMPTY) @@ -65,7 +66,12 @@ class VideoPlaybackService : MediaSessionService() { addSession(mediaSession) val notificationId = player.hashCode() - startForeground(notificationId, buildNotification(mediaSession)) + try { + startForeground(notificationId, buildNotification(mediaSession)) + isForeground = true + } catch (e: Exception) { + DebugLog.e(TAG, "Failed to start foreground service: " + e.message) + } } fun unregisterPlayer(player: ExoPlayer) { @@ -249,8 +255,13 @@ class VideoPlaybackService : MediaSessionService() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startForeground(PLACEHOLDER_NOTIFICATION_ID, createPlaceholderNotification()) + try { + if (!isForeground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startForeground(PLACEHOLDER_NOTIFICATION_ID, createPlaceholderNotification()) + isForeground = true + } + } catch (e: Exception) { + DebugLog.e(TAG, "Failed to start foreground service: " + e.message) } intent?.let {