diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/AbMediaDescriptionAdapter.kt b/android/app/src/main/java/com/audiobookshelf/app/player/AbMediaDescriptionAdapter.kt index eaf580bd..8a19db5e 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/AbMediaDescriptionAdapter.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/AbMediaDescriptionAdapter.kt @@ -14,7 +14,7 @@ import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.ui.PlayerNotificationManager import kotlinx.coroutines.* -class AbMediaDescriptionAdapter constructor(private val controller: MediaControllerCompat, private val playerNotificationService: PlayerNotificationService) : PlayerNotificationManager.MediaDescriptionAdapter { +class AbMediaDescriptionAdapter (private val controller: MediaControllerCompat, private val playerNotificationService: PlayerNotificationService) : PlayerNotificationManager.MediaDescriptionAdapter { private val tag = "MediaDescriptionAdapter" private var currentIconUri: Uri? = null @@ -35,6 +35,12 @@ class AbMediaDescriptionAdapter constructor(private val controller: MediaControl callback: PlayerNotificationManager.BitmapCallback ): Bitmap? { val albumArtUri = controller.metadata.description.iconUri + val albumBitmap = controller.metadata.description.iconBitmap + + // For local cover images, bitmap is set in PlayerNotificationService TimelineQueueNavigator.getMediaDescription + if (albumBitmap != null) { + return albumBitmap + } return if (currentIconUri != albumArtUri || currentBitmap == null) { // Cache the bitmap for the current audiobook so that successive calls to @@ -59,7 +65,6 @@ class AbMediaDescriptionAdapter constructor(private val controller: MediaControl } null } - } else { currentBitmap } 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 cf61c145..e37e7147 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 @@ -4,11 +4,14 @@ import android.annotation.SuppressLint import android.app.* import android.content.Context import android.content.Intent +import android.graphics.Bitmap import android.graphics.Color +import android.graphics.ImageDecoder import android.hardware.Sensor import android.hardware.SensorManager import android.net.* import android.os.* +import android.provider.MediaStore import android.provider.Settings import android.support.v4.media.MediaBrowserCompat import android.support.v4.media.MediaDescriptionCompat @@ -308,6 +311,22 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { val coverUri = currentPlaybackSession!!.getCoverUri(ctx) + + var bitmap: Bitmap? = null + // Local covers get bitmap + // Note: In Android Auto for local cover images, setting the icon uri to a local path does not work (cover is blank) + // so we create and set the bitmap here instead of AbMediaDescriptionAdapter + if (currentPlaybackSession!!.localLibraryItem?.coverContentUrl != null) { + bitmap = + if (Build.VERSION.SDK_INT < 28) { + MediaStore.Images.Media.getBitmap(ctx.contentResolver, coverUri) + } else { + val source: ImageDecoder.Source = + ImageDecoder.createSource(ctx.contentResolver, coverUri) + ImageDecoder.decodeBitmap(source) + } + } + // Fix for local images crashing on Android 11 for specific devices // https://stackoverflow.com/questions/64186578/android-11-mediastyle-notification-crash/64232958#64232958 try { @@ -331,7 +350,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { .setExtras(extra) .setTitle(currentPlaybackSession!!.displayTitle) - mediaDescriptionBuilder.setIconUri(coverUri) + bitmap?.let { mediaDescriptionBuilder.setIconBitmap(it) } + ?: mediaDescriptionBuilder.setIconUri(coverUri) return mediaDescriptionBuilder.build() }