fix: cache media cover to avoid vanishing on swipe

This commit is contained in:
2026-08-17 12:49:38 +08:00
parent 17d573e589
commit 8d7e015857
@@ -38,6 +38,12 @@ class MediaSessionSource(context: Context) {
// only update in the notification
private val notificationTitles = mutableMapOf<String, String>()
// 封面缓存(packageName -> base64):部分 App 切歌瞬间 ART 为 null,用缓存兜底,
// 避免媒体卡横划切换后封面消失
// Cover cache (packageName -> base64): some apps briefly return null ART on track
// switch; fall back to the cached cover so the card cover doesn't vanish on swipe
private val coverCache = mutableMapOf<String, String>()
init {
HearthNotificationListenerService.onMediaTitle = { pkg, title ->
notificationTitles[pkg] = title
@@ -222,12 +228,19 @@ class MediaSessionSource(context: Context) {
val duration = meta?.getLong(MediaMetadata.METADATA_KEY_DURATION) ?: 0L
val position = state?.position ?: 0L
val playing = state?.state == PlaybackState.STATE_PLAYING
Log.d(TAG, "media: ${c.packageName} duration=$duration position=$position playing=$playing state=${state?.state}")
// 封面:优先当前 ART,为空时用缓存兜底(切歌瞬间 ART 可能为 null)
// Cover: prefer current ART, fall back to cache when empty (ART may be null
// briefly on track switch)
val cover = meta?.getBitmap(MediaMetadata.METADATA_KEY_ART)?.let { bitmapToBase64(it) }
?: coverCache[c.packageName]
?: ""
if (cover.isNotEmpty()) coverCache[c.packageName] = cover
Log.d(TAG, "media: ${c.packageName} duration=$duration position=$position playing=$playing cover=${cover.isNotEmpty()}")
return MediaInfo(
title,
meta?.getString(MediaMetadata.METADATA_KEY_ARTIST) ?: "",
meta?.getString(MediaMetadata.METADATA_KEY_ALBUM) ?: "",
meta?.getBitmap(MediaMetadata.METADATA_KEY_ART)?.let { bitmapToBase64(it) } ?: "",
cover,
playing,
position,
duration,