From 4bf54f111d27cc55913d339717fc68db2574038c Mon Sep 17 00:00:00 2001 From: zoro Date: Mon, 1 Jun 2026 18:42:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A7=8B=E7=BB=88=E4=BC=98=E5=85=88=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=B5=8C=E5=85=A5=E5=BC=8F=E5=B0=81=E9=9D=A2=EF=BC=8C?= =?UTF-8?q?=E5=86=8D=E5=9B=9E=E9=80=80=E5=88=B0=20MediaStore=20=E5=92=8C?= =?UTF-8?q?=E7=BD=91=E7=BB=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 hasAnyLocalCover() 快速检查音频文件嵌入式封面 - fetch(song) 始终优先尝试嵌入式封面(不受 ignoreMediaStore 控制) - MediaStore 仅当 ignoreMediaStore=false 且 albumId>0 时使用 - 确保所有歌曲都能走到网络下载路径 --- .../java/remix/myplayer/glide/UriFetcher.kt | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/remix/myplayer/glide/UriFetcher.kt b/app/src/main/java/remix/myplayer/glide/UriFetcher.kt index 7c5e7998..e3b03da5 100644 --- a/app/src/main/java/remix/myplayer/glide/UriFetcher.kt +++ b/app/src/main/java/remix/myplayer/glide/UriFetcher.kt @@ -2,6 +2,7 @@ package remix.myplayer.glide import android.content.ContentUris import android.content.Context +import android.media.MediaMetadataRetriever import android.net.Uri import android.provider.MediaStore.Audio import androidx.core.net.toUri @@ -122,8 +123,9 @@ class UriFetcher @Inject constructor( } } if (song.isLocal()) { // 仅本地歌曲 - if (song.albumId <= 0 || song.id <= 0) { - return Uri.EMPTY + // 1. 始终优先尝试嵌入式封面(直接从音频文件读取,无需 albumId) + if (song.data.isNotEmpty() && hasAnyLocalCover(song.data)) { + return (PREFIX_EMBEDDED + song.data).toUri() } // 自定义封面 // val customArtFile = getCustomThumbIfExist(song.albumId, Constants.ALBUM) @@ -131,14 +133,11 @@ class UriFetcher @Inject constructor( // return Uri.fromFile(customArtFile) // } - // 内置 - if (ignoreMediaStore()) { - val songs = songRepo.getSongs(Audio.Media._ID + "=" + song.id, null) - if (songs.isNotEmpty()) { - return (PREFIX_EMBEDDED + songs[0].data).toUri() + // 2. MediaStore 封面(仅当未禁用 MediaStore 且 albumId 有效时) + if (!ignoreMediaStore() && song.albumId > 0 && song.id > 0) { + if (isAlbumThumbExistInMediaCache(song.artUri)) { + return song.artUri } - } else if (isAlbumThumbExistInMediaCache(song.artUri)) { - return song.artUri } } @@ -382,6 +381,23 @@ class UriFetcher @Inject constructor( return null } + /** + * 快速检查音频文件是否存在嵌入式封面图 + */ + private fun hasAnyLocalCover(filePath: String): Boolean { + try { + val retriever = MediaMetadataRetriever() + try { + retriever.setDataSource(filePath) + return retriever.embeddedPicture != null + } finally { + retriever.release() + } + } catch (_: Exception) { + } + return false + } + private fun getSearchKey(model: Any): String? { return SearchKeyUtil.getSearchKeys(model).firstOrNull()?.value }