diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationListener.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationListener.kt index 3fa291c4..de264654 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationListener.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationListener.kt @@ -1,21 +1,37 @@ package com.audiobookshelf.app.player import android.app.Notification +import android.content.pm.ServiceInfo +import android.os.Build import android.util.Log import com.google.android.exoplayer2.ui.PlayerNotificationManager class PlayerNotificationListener(var playerNotificationService:PlayerNotificationService) : PlayerNotificationManager.NotificationListener { var tag = "PlayerNotificationListener" + companion object { + var isForegroundService = false + } + override fun onNotificationPosted( notificationId: Int, notification: Notification, onGoing: Boolean) { - // Start foreground service - Log.d(tag, "Notification Posted $notificationId - Start Foreground | $notification") - PlayerNotificationService.isClosed = false - playerNotificationService.startForeground(notificationId, notification) + if (onGoing && !isForegroundService) { + // Start foreground service + Log.d(tag, "Notification Posted $notificationId - Start Foreground | $notification") + PlayerNotificationService.isClosed = false + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + playerNotificationService.startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK) + } else { + playerNotificationService.startForeground(notificationId, notification) + } + isForegroundService = true + } else { + Log.d(tag, "Notification posted $notificationId, not starting foreground - onGoing=$onGoing | isForegroundService=$isForegroundService") + } } override fun onNotificationCancelled( @@ -39,5 +55,6 @@ class PlayerNotificationListener(var playerNotificationService:PlayerNotificatio PlayerNotificationService.isSwitchingPlayer = false } } + isForegroundService = false } } diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt index 117a350b..71f12af6 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt @@ -175,7 +175,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { castPlayer?.release() mediaSession.release() mediaProgressSyncer.reset() - Log.d(tag, "onDestroy") isStarted = false super.onDestroy() diff --git a/android/variables.gradle b/android/variables.gradle index 57d4eb98..a7a3183a 100644 --- a/android/variables.gradle +++ b/android/variables.gradle @@ -14,7 +14,7 @@ ext { cordovaAndroidVersion = '10.1.1' androidx_car_version = '1.0.0-alpha7' androidx_core_ktx_version = '1.7.0' - androidx_media_version = '1.5.0' + androidx_media_version = '1.6.0' androidx_preference_version = '1.1.1' androidx_test_runner_version = '1.3.0' arch_lifecycle_version = '2.2.0'