增加音乐管理列表、播放条封面显示,调整封面加载逻辑,优化一下列表数据显示字段
This commit is contained in:
parent
9f3f5a69af
commit
b75be79755
@ -12,6 +12,7 @@ import androidx.room.FtsOptions
|
|||||||
import androidx.room.Index
|
import androidx.room.Index
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.RewriteQueriesToDropUnusedColumns
|
||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import androidx.room.Transaction
|
import androidx.room.Transaction
|
||||||
@ -62,7 +63,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY title COLLATE NOCASE ASC
|
ORDER BY title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun pagedByTrack(): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun pagedByTrack(): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -72,7 +74,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY music_tracks.title COLLATE NOCASE ASC
|
ORDER BY music_tracks.title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun searchPagedByTrack(query: String): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun searchPagedByTrack(query: String): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -98,7 +101,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY album COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
ORDER BY album COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun pagedByAlbum(): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun pagedByAlbum(): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -108,7 +112,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY music_tracks.album COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
ORDER BY music_tracks.album COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun searchPagedByAlbum(query: String): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun searchPagedByAlbum(query: String): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -134,7 +139,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY artist COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
ORDER BY artist COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun pagedByArtist(): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun pagedByArtist(): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -144,7 +150,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY music_tracks.artist COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
ORDER BY music_tracks.artist COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun searchPagedByArtist(query: String): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun searchPagedByArtist(query: String): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -170,7 +177,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY date_modified_seconds DESC, title COLLATE NOCASE ASC
|
ORDER BY date_modified_seconds DESC, title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun pagedByRecent(): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun pagedByRecent(): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -180,7 +188,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY music_tracks.date_modified_seconds DESC, music_tracks.title COLLATE NOCASE ASC
|
ORDER BY music_tracks.date_modified_seconds DESC, music_tracks.title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun searchPagedByRecent(query: String): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun searchPagedByRecent(query: String): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -206,7 +215,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY format COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
ORDER BY format COLLATE NOCASE ASC, title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun pagedByFormat(): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun pagedByFormat(): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -216,7 +226,8 @@ interface MusicTrackDao {
|
|||||||
ORDER BY music_tracks.format COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
ORDER BY music_tracks.format COLLATE NOCASE ASC, music_tracks.title COLLATE NOCASE ASC
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun searchPagedByFormat(query: String): PagingSource<Int, MusicTrackEntity>
|
@RewriteQueriesToDropUnusedColumns
|
||||||
|
fun searchPagedByFormat(query: String): PagingSource<Int, MusicTrackListEntity>
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
@ -239,7 +250,8 @@ interface MusicTrackDao {
|
|||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
SELECT id, uri, title, artist, album, mime_type, format, sample_rate_hz, bitrate,
|
SELECT id, uri, title, artist, album, mime_type, format, sample_rate_hz, bitrate,
|
||||||
bit_depth, duration_ms, source_path, is_playable, requires_ffmpeg, unavailable_reason
|
bit_depth, duration_ms, source_path, is_cover_edited, is_playable,
|
||||||
|
requires_ffmpeg, unavailable_reason
|
||||||
FROM music_tracks
|
FROM music_tracks
|
||||||
WHERE id = :id
|
WHERE id = :id
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
@ -247,6 +259,9 @@ interface MusicTrackDao {
|
|||||||
)
|
)
|
||||||
suspend fun playbackTrackById(id: Long): PlaybackTrackEntity?
|
suspend fun playbackTrackById(id: Long): PlaybackTrackEntity?
|
||||||
|
|
||||||
|
@Query("UPDATE music_tracks SET is_cover_edited = :isCoverEdited WHERE id = :id")
|
||||||
|
suspend fun updateCoverEdited(id: Long, isCoverEdited: Boolean)
|
||||||
|
|
||||||
@Query("SELECT COUNT(*) FROM music_tracks")
|
@Query("SELECT COUNT(*) FROM music_tracks")
|
||||||
suspend fun trackCount(): Int
|
suspend fun trackCount(): Int
|
||||||
|
|
||||||
@ -305,13 +320,16 @@ interface MusicTrackDao {
|
|||||||
if (tracks.isEmpty()) return
|
if (tracks.isEmpty()) return
|
||||||
val existingTracks = tracksBySourcePaths(tracks.map { it.sourcePath })
|
val existingTracks = tracksBySourcePaths(tracks.map { it.sourcePath })
|
||||||
.associateBy { it.sourcePath }
|
.associateBy { it.sourcePath }
|
||||||
upsertTracksInternal(
|
upsertTracksInternal(
|
||||||
tracks.map { track ->
|
tracks.map { track ->
|
||||||
existingTracks[track.sourcePath]?.let { existing ->
|
existingTracks[track.sourcePath]?.let { existing ->
|
||||||
track.copy(id = existing.id)
|
track.copy(
|
||||||
} ?: track
|
id = existing.id,
|
||||||
}
|
isCoverEdited = existing.isCoverEdited
|
||||||
)
|
)
|
||||||
|
} ?: track
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Query("DELETE FROM music_tracks")
|
@Query("DELETE FROM music_tracks")
|
||||||
@ -358,7 +376,27 @@ data class MusicTrackEntity(
|
|||||||
@ColumnInfo(name = "is_tag_readable") val isTagReadable: Boolean = true,
|
@ColumnInfo(name = "is_tag_readable") val isTagReadable: Boolean = true,
|
||||||
@ColumnInfo(name = "is_tag_writable") val isTagWritable: Boolean = false,
|
@ColumnInfo(name = "is_tag_writable") val isTagWritable: Boolean = false,
|
||||||
@ColumnInfo(name = "unavailable_reason") val unavailableReason: String? = null,
|
@ColumnInfo(name = "unavailable_reason") val unavailableReason: String? = null,
|
||||||
@ColumnInfo(name = "parse_failure_reason") val parseFailureReason: String? = null
|
@ColumnInfo(name = "parse_failure_reason") val parseFailureReason: String? = null,
|
||||||
|
@ColumnInfo(name = "is_cover_edited") val isCoverEdited: Boolean = false
|
||||||
|
)
|
||||||
|
|
||||||
|
data class MusicTrackListEntity(
|
||||||
|
val id: Long,
|
||||||
|
val uri: String,
|
||||||
|
val title: String,
|
||||||
|
val artist: String,
|
||||||
|
val album: String,
|
||||||
|
@ColumnInfo(name = "size_bytes")
|
||||||
|
val sizeBytes: Long,
|
||||||
|
val format: String,
|
||||||
|
@ColumnInfo(name = "sample_rate_hz")
|
||||||
|
val sampleRateHz: Int?,
|
||||||
|
@ColumnInfo(name = "bit_depth")
|
||||||
|
val bitDepth: String?,
|
||||||
|
@ColumnInfo(name = "source_path")
|
||||||
|
val sourcePath: String,
|
||||||
|
@ColumnInfo(name = "is_cover_edited")
|
||||||
|
val isCoverEdited: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
@Fts4(
|
@Fts4(
|
||||||
@ -393,6 +431,8 @@ data class PlaybackTrackEntity(
|
|||||||
val durationMs: Long,
|
val durationMs: Long,
|
||||||
@ColumnInfo(name = "source_path")
|
@ColumnInfo(name = "source_path")
|
||||||
val sourcePath: String,
|
val sourcePath: String,
|
||||||
|
@ColumnInfo(name = "is_cover_edited")
|
||||||
|
val isCoverEdited: Boolean,
|
||||||
@ColumnInfo(name = "is_playable")
|
@ColumnInfo(name = "is_playable")
|
||||||
val isPlayable: Boolean,
|
val isPlayable: Boolean,
|
||||||
@ColumnInfo(name = "requires_ffmpeg")
|
@ColumnInfo(name = "requires_ffmpeg")
|
||||||
@ -423,7 +463,29 @@ fun MusicTrackEntity.toDomain(): ScannedAudioTrack {
|
|||||||
isTagReadable = isTagReadable,
|
isTagReadable = isTagReadable,
|
||||||
isTagWritable = isTagWritable,
|
isTagWritable = isTagWritable,
|
||||||
unavailableReason = unavailableReason,
|
unavailableReason = unavailableReason,
|
||||||
parseFailureReason = parseFailureReason
|
parseFailureReason = parseFailureReason,
|
||||||
|
isCoverEdited = isCoverEdited
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MusicTrackListEntity.toDomain(): ScannedAudioTrack {
|
||||||
|
return ScannedAudioTrack(
|
||||||
|
id = id,
|
||||||
|
uri = Uri.parse(uri),
|
||||||
|
title = title,
|
||||||
|
artist = artist,
|
||||||
|
album = album,
|
||||||
|
durationMs = 0L,
|
||||||
|
sizeBytes = sizeBytes,
|
||||||
|
mimeType = "",
|
||||||
|
format = format,
|
||||||
|
sampleRateHz = sampleRateHz,
|
||||||
|
bitrate = null,
|
||||||
|
bitDepth = bitDepth,
|
||||||
|
relativePath = null,
|
||||||
|
sourcePath = sourcePath,
|
||||||
|
dateModifiedSeconds = 0L,
|
||||||
|
isCoverEdited = isCoverEdited
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -441,6 +503,7 @@ fun PlaybackTrackEntity.toDomain(): PlaybackTrack {
|
|||||||
bitDepth = bitDepth,
|
bitDepth = bitDepth,
|
||||||
durationMs = durationMs.takeIf { it > 0L },
|
durationMs = durationMs.takeIf { it > 0L },
|
||||||
sourcePath = sourcePath,
|
sourcePath = sourcePath,
|
||||||
|
isCoverEdited = isCoverEdited,
|
||||||
isPlayable = isPlayable,
|
isPlayable = isPlayable,
|
||||||
requiresFfmpeg = requiresFfmpeg,
|
requiresFfmpeg = requiresFfmpeg,
|
||||||
unavailableReason = unavailableReason
|
unavailableReason = unavailableReason
|
||||||
@ -470,7 +533,8 @@ fun ScannedAudioTrack.toEntity(): MusicTrackEntity {
|
|||||||
isTagReadable = isTagReadable,
|
isTagReadable = isTagReadable,
|
||||||
isTagWritable = isTagWritable,
|
isTagWritable = isTagWritable,
|
||||||
unavailableReason = unavailableReason,
|
unavailableReason = unavailableReason,
|
||||||
parseFailureReason = parseFailureReason
|
parseFailureReason = parseFailureReason,
|
||||||
|
isCoverEdited = isCoverEdited
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -891,8 +891,11 @@ class AudioPlaybackController(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun displayQueueItems(): List<PlaybackQueueItem> {
|
private fun displayQueueItems(): List<PlaybackQueueItem> {
|
||||||
|
if (queueIds.isEmpty()) return emptyList()
|
||||||
|
if (playMode != PlayMode.Shuffle && queueItems.size == queueIds.size) {
|
||||||
|
return queueItems
|
||||||
|
}
|
||||||
val indices = displayQueueIndices()
|
val indices = displayQueueIndices()
|
||||||
if (indices.isEmpty()) return emptyList()
|
|
||||||
return indices.map { index ->
|
return indices.map { index ->
|
||||||
queueItems.getOrNull(index) ?: PlaybackQueueItem(id = queueIds[index])
|
queueItems.getOrNull(index) ?: PlaybackQueueItem(id = queueIds[index])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ data class PlaybackTrack(
|
|||||||
val bitDepth: String? = null,
|
val bitDepth: String? = null,
|
||||||
val durationMs: Long? = null,
|
val durationMs: Long? = null,
|
||||||
val sourcePath: String? = null,
|
val sourcePath: String? = null,
|
||||||
|
val isCoverEdited: Boolean = false,
|
||||||
val isPlayable: Boolean = true,
|
val isPlayable: Boolean = true,
|
||||||
val requiresFfmpeg: Boolean = false,
|
val requiresFfmpeg: Boolean = false,
|
||||||
val unavailableReason: String? = null
|
val unavailableReason: String? = null
|
||||||
|
|||||||
@ -23,7 +23,8 @@ data class ScannedAudioTrack(
|
|||||||
val isTagReadable: Boolean = defaultCapabilityFor(format).isTagReadable,
|
val isTagReadable: Boolean = defaultCapabilityFor(format).isTagReadable,
|
||||||
val isTagWritable: Boolean = defaultCapabilityFor(format).isTagWritable,
|
val isTagWritable: Boolean = defaultCapabilityFor(format).isTagWritable,
|
||||||
val unavailableReason: String? = defaultCapabilityFor(format).unavailableReason,
|
val unavailableReason: String? = defaultCapabilityFor(format).unavailableReason,
|
||||||
val parseFailureReason: String? = null
|
val parseFailureReason: String? = null,
|
||||||
|
val isCoverEdited: Boolean = false
|
||||||
) {
|
) {
|
||||||
val isHighResolution: Boolean
|
val isHighResolution: Boolean
|
||||||
get() = (sampleRateHz ?: 0) >= 88_200 || bitDepth in highBitDepthLabels || format in dsdFormats
|
get() = (sampleRateHz ?: 0) >= 88_200 || bitDepth in highBitDepthLabels || format in dsdFormats
|
||||||
|
|||||||
@ -82,6 +82,7 @@ import com.audio.vibe.ui.components.VibeScreen
|
|||||||
import com.audio.vibe.ui.i18n.AppStrings
|
import com.audio.vibe.ui.i18n.AppStrings
|
||||||
import com.audio.vibe.ui.i18n.LocalAppStrings
|
import com.audio.vibe.ui.i18n.LocalAppStrings
|
||||||
import com.audio.vibe.ui.player.MiniPlayerBar
|
import com.audio.vibe.ui.player.MiniPlayerBar
|
||||||
|
import com.audio.vibe.ui.player.CoverImage
|
||||||
import com.audio.vibe.ui.theme.AudioVibeTheme
|
import com.audio.vibe.ui.theme.AudioVibeTheme
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -671,15 +672,35 @@ private fun TrackRow(
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(48.dp)
|
.size(48.dp)
|
||||||
.clip(RoundedCornerShape(12.dp))
|
.clip(RoundedCornerShape(12.dp))
|
||||||
.background(if (track.isHighResolution) AudioVibeColors.Primary else AudioVibeColors.DarkPanel),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
) {
|
||||||
Icon(
|
CoverImage(
|
||||||
if (track.isHighResolution) Icons.Default.Verified else Icons.Default.AudioFile,
|
trackId = track.id,
|
||||||
null,
|
sourcePath = track.sourcePath,
|
||||||
tint = Color.White,
|
uri = track.uri,
|
||||||
modifier = Modifier.size(24.dp)
|
isCoverEdited = track.isCoverEdited,
|
||||||
|
cornerRadius = 12.dp,
|
||||||
|
placeholderIconSize = 22.dp,
|
||||||
|
decodeSize = 56.dp,
|
||||||
|
animateChanges = false,
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
)
|
)
|
||||||
|
if (track.isHighResolution) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.align(Alignment.BottomEnd)
|
||||||
|
.size(18.dp)
|
||||||
|
.clip(RoundedCornerShape(7.dp))
|
||||||
|
.background(AudioVibeColors.Primary),
|
||||||
|
contentAlignment = Alignment.Center
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Default.Verified,
|
||||||
|
contentDescription = null,
|
||||||
|
tint = Color.White,
|
||||||
|
modifier = Modifier.size(12.dp)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Column(
|
Column(
|
||||||
Modifier
|
Modifier
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.audio.vibe.ui.musicmanager
|
package com.audio.vibe.ui.musicmanager
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
@ -57,10 +58,12 @@ class MusicManagerViewModel(
|
|||||||
refreshLibrarySummaryForQuery(event.query)
|
refreshLibrarySummaryForQuery(event.query)
|
||||||
refreshLocateTrackIndex()
|
refreshLocateTrackIndex()
|
||||||
}
|
}
|
||||||
|
|
||||||
is MusicManagerEvent.CategoryChanged -> {
|
is MusicManagerEvent.CategoryChanged -> {
|
||||||
_uiState.update { it.copy(category = event.category) }
|
_uiState.update { it.copy(category = event.category) }
|
||||||
refreshLocateTrackIndex()
|
refreshLocateTrackIndex()
|
||||||
}
|
}
|
||||||
|
|
||||||
is MusicManagerEvent.PlayToggled -> togglePlayback(event.trackId)
|
is MusicManagerEvent.PlayToggled -> togglePlayback(event.trackId)
|
||||||
MusicManagerEvent.MiniPlayerPlayPauseToggled -> toggleMiniPlayer()
|
MusicManagerEvent.MiniPlayerPlayPauseToggled -> toggleMiniPlayer()
|
||||||
MusicManagerEvent.MiniPlayerSkipNext -> skipNext()
|
MusicManagerEvent.MiniPlayerSkipNext -> skipNext()
|
||||||
@ -92,6 +95,10 @@ class MusicManagerViewModel(
|
|||||||
|
|
||||||
private suspend fun observePlayback(controller: PlaybackServiceController) {
|
private suspend fun observePlayback(controller: PlaybackServiceController) {
|
||||||
controller.state.collect { playbackState ->
|
controller.state.collect { playbackState ->
|
||||||
|
Log.i(
|
||||||
|
"observePlayback",
|
||||||
|
"currentId: ${playbackState.currentId} isPlaying: ${playbackState.isPlaying}"
|
||||||
|
)
|
||||||
_uiState.update { state ->
|
_uiState.update { state ->
|
||||||
state.copy(
|
state.copy(
|
||||||
playingId = playbackState.currentId.takeIf { playbackState.isPlaying },
|
playingId = playbackState.currentId.takeIf { playbackState.isPlaying },
|
||||||
@ -188,7 +195,14 @@ class MusicManagerViewModel(
|
|||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val query = _uiState.value.query
|
val query = _uiState.value.query
|
||||||
val summary = repository.loadLibrarySummary(query)
|
val summary = repository.loadLibrarySummary(query)
|
||||||
_uiState.update { it.copy(libraryState = summary.toLibraryState(fromCache = true, query = query)) }
|
_uiState.update {
|
||||||
|
it.copy(
|
||||||
|
libraryState = summary.toLibraryState(
|
||||||
|
fromCache = true,
|
||||||
|
query = query
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,7 +226,12 @@ class MusicManagerViewModel(
|
|||||||
val query = _uiState.value.query
|
val query = _uiState.value.query
|
||||||
val summary = repository.loadLibrarySummary(query)
|
val summary = repository.loadLibrarySummary(query)
|
||||||
_uiState.update { state ->
|
_uiState.update { state ->
|
||||||
state.copy(libraryState = summary.toLibraryState(fromCache = false, query = query))
|
state.copy(
|
||||||
|
libraryState = summary.toLibraryState(
|
||||||
|
fromCache = false,
|
||||||
|
query = query
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch (_: CancellationException) {
|
} catch (_: CancellationException) {
|
||||||
// Reset and a new scan both cancel the old scan job intentionally.
|
// Reset and a new scan both cancel the old scan job intentionally.
|
||||||
@ -261,7 +280,12 @@ class MusicManagerViewModel(
|
|||||||
val query = _uiState.value.query
|
val query = _uiState.value.query
|
||||||
val summary = repository.loadLibrarySummary(query)
|
val summary = repository.loadLibrarySummary(query)
|
||||||
_uiState.update { state ->
|
_uiState.update { state ->
|
||||||
state.copy(libraryState = summary.toLibraryState(fromCache = false, query = query))
|
state.copy(
|
||||||
|
libraryState = summary.toLibraryState(
|
||||||
|
fromCache = false,
|
||||||
|
query = query
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch (_: CancellationException) {
|
} catch (_: CancellationException) {
|
||||||
// Reset and a new scan both cancel the old scan job intentionally.
|
// Reset and a new scan both cancel the old scan job intentionally.
|
||||||
@ -331,11 +355,21 @@ class MusicManagerViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
MusicLibraryState.Empty -> {
|
MusicLibraryState.Empty -> {
|
||||||
state.copy(libraryState = summary.toLibraryState(fromCache = true, query = query))
|
state.copy(
|
||||||
|
libraryState = summary.toLibraryState(
|
||||||
|
fromCache = true,
|
||||||
|
query = query
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
MusicLibraryState.Loading -> {
|
MusicLibraryState.Loading -> {
|
||||||
state.copy(libraryState = summary.toLibraryState(fromCache = true, query = query))
|
state.copy(
|
||||||
|
libraryState = summary.toLibraryState(
|
||||||
|
fromCache = true,
|
||||||
|
query = query
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
is MusicLibraryState.Error -> state
|
is MusicLibraryState.Error -> state
|
||||||
@ -437,7 +471,10 @@ private data class TrackPagingQuery(
|
|||||||
val sort: MusicLibrarySort
|
val sort: MusicLibrarySort
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun MusicLibrarySummary.toLibraryState(fromCache: Boolean, query: String): MusicLibraryState {
|
private fun MusicLibrarySummary.toLibraryState(
|
||||||
|
fromCache: Boolean,
|
||||||
|
query: String
|
||||||
|
): MusicLibraryState {
|
||||||
return if (totalTracks == 0 && query.isBlank()) {
|
return if (totalTracks == 0 && query.isBlank()) {
|
||||||
MusicLibraryState.Empty
|
MusicLibraryState.Empty
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
package com.audio.vibe.ui.player
|
package com.audio.vibe.ui.player
|
||||||
|
|
||||||
import android.graphics.BitmapFactory
|
import android.content.Context
|
||||||
import android.media.MediaMetadataRetriever
|
import android.graphics.Bitmap
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.compose.animation.Crossfade
|
import android.os.Build
|
||||||
import androidx.compose.animation.core.tween
|
import android.util.Size as AndroidSize
|
||||||
import androidx.compose.foundation.Image
|
import android.webkit.MimeTypeMap
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
@ -15,74 +15,136 @@ import androidx.compose.material.icons.Icons
|
|||||||
import androidx.compose.material.icons.filled.MusicNote
|
import androidx.compose.material.icons.filled.MusicNote
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.graphics.Brush
|
import androidx.compose.ui.graphics.Brush
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import coil3.ImageLoader
|
||||||
|
import coil3.annotation.ExperimentalCoilApi
|
||||||
|
import coil3.compose.AsyncImage
|
||||||
|
import coil3.decode.DataSource
|
||||||
|
import coil3.decode.ImageSource
|
||||||
|
import coil3.disk.DiskCache
|
||||||
|
import coil3.fetch.FetchResult
|
||||||
|
import coil3.fetch.Fetcher
|
||||||
|
import coil3.fetch.ImageFetchResult
|
||||||
|
import coil3.fetch.SourceFetchResult
|
||||||
|
import coil3.key.Keyer
|
||||||
|
import coil3.memory.MemoryCache
|
||||||
|
import coil3.request.ImageRequest
|
||||||
|
import coil3.request.crossfade
|
||||||
|
import coil3.size.Dimension
|
||||||
|
import coil3.asImage
|
||||||
|
import com.arthenica.ffmpegkit.FFmpegKit
|
||||||
|
import com.arthenica.ffmpegkit.FFmpegKitConfig
|
||||||
|
import com.arthenica.ffmpegkit.FFprobeKit
|
||||||
|
import com.audio.vibe.data.local.MusicLibraryDatabase
|
||||||
import com.audio.vibe.ui.components.AudioVibeColors
|
import com.audio.vibe.ui.components.AudioVibeColors
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.ensureActive
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import okio.FileSystem
|
||||||
|
import okio.Path.Companion.toOkioPath
|
||||||
|
import org.json.JSONObject
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CoverImage(
|
fun CoverImage(
|
||||||
|
trackId: Long? = null,
|
||||||
sourcePath: String?,
|
sourcePath: String?,
|
||||||
uri: Uri?,
|
uri: Uri?,
|
||||||
|
isCoverEdited: Boolean = false,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
cornerRadius: androidx.compose.ui.unit.Dp = 20.dp
|
cornerRadius: Dp = 20.dp,
|
||||||
|
placeholderIconSize: Dp = 64.dp,
|
||||||
|
decodeSize: Dp? = null,
|
||||||
|
animateChanges: Boolean = true,
|
||||||
|
cacheToken: Any? = null
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val shape = RoundedCornerShape(cornerRadius)
|
val density = LocalDensity.current
|
||||||
var artwork by remember { mutableStateOf<CoverArtwork?>(null) }
|
val imageLoader = remember(context) {
|
||||||
var requestedKey by remember { mutableStateOf<String?>(null) }
|
AudioCoverImageLoader.get(context.applicationContext)
|
||||||
|
}
|
||||||
LaunchedEffect(sourcePath, uri) {
|
val decodeSizePx = remember(decodeSize, density) {
|
||||||
val key = sourcePath ?: uri?.toString()
|
decodeSize?.let { with(density) { it.toPx().roundToInt().coerceAtLeast(1) } }
|
||||||
requestedKey = key
|
}
|
||||||
val resolved = withContext(Dispatchers.IO) {
|
val model = remember(
|
||||||
resolveCoverArtwork(context, sourcePath, uri)
|
context,
|
||||||
}
|
trackId,
|
||||||
if (requestedKey == key) {
|
sourcePath,
|
||||||
artwork = resolved
|
uri,
|
||||||
}
|
isCoverEdited,
|
||||||
|
decodeSizePx,
|
||||||
|
animateChanges,
|
||||||
|
cacheToken
|
||||||
|
) {
|
||||||
|
val data = AudioCoverModel(
|
||||||
|
trackId = trackId,
|
||||||
|
sourcePath = sourcePath,
|
||||||
|
uri = uri,
|
||||||
|
isCoverEdited = isCoverEdited,
|
||||||
|
cacheToken = cacheToken
|
||||||
|
)
|
||||||
|
ImageRequest.Builder(context)
|
||||||
|
.data(data)
|
||||||
|
.memoryCacheKey(data.cacheKey)
|
||||||
|
.diskCacheKey(data.cacheKey)
|
||||||
|
.crossfade(if (animateChanges) COVER_CROSSFADE_MS else 0)
|
||||||
|
.apply {
|
||||||
|
if (decodeSizePx != null) {
|
||||||
|
size(decodeSizePx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
Box(modifier = modifier.clip(shape)) {
|
Box(modifier = modifier.clip(RoundedCornerShape(cornerRadius))) {
|
||||||
DefaultCoverPlaceholder(
|
DefaultCoverPlaceholder(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
cornerRadius = 0.dp
|
cornerRadius = 0.dp,
|
||||||
|
iconSize = placeholderIconSize
|
||||||
)
|
)
|
||||||
Crossfade(
|
AsyncImage(
|
||||||
targetState = artwork,
|
model = model,
|
||||||
animationSpec = tween(durationMillis = 260),
|
imageLoader = imageLoader,
|
||||||
label = "coverArtwork"
|
contentDescription = "专辑封面",
|
||||||
) { currentArtwork ->
|
modifier = Modifier.fillMaxSize(),
|
||||||
if (currentArtwork != null) {
|
contentScale = ContentScale.Crop
|
||||||
Image(
|
)
|
||||||
bitmap = currentArtwork.bitmap.asImageBitmap(),
|
}
|
||||||
contentDescription = "专辑封面",
|
}
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
contentScale = ContentScale.Crop
|
@OptIn(ExperimentalCoilApi::class)
|
||||||
)
|
fun invalidateAudioCoverCache(
|
||||||
}
|
context: Context,
|
||||||
}
|
sourcePath: String?,
|
||||||
|
uri: Uri?,
|
||||||
|
cacheToken: Any? = null
|
||||||
|
) {
|
||||||
|
val imageLoader = AudioCoverImageLoader.get(context.applicationContext)
|
||||||
|
listOf(false, true).forEach { isCoverEdited ->
|
||||||
|
val cacheKey = audioCoverCacheKey(sourcePath, uri, isCoverEdited, cacheToken)
|
||||||
|
imageLoader.memoryCache?.remove(MemoryCache.Key(cacheKey))
|
||||||
|
imageLoader.diskCache?.remove(cacheKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DefaultCoverPlaceholder(
|
fun DefaultCoverPlaceholder(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
cornerRadius: androidx.compose.ui.unit.Dp = 20.dp
|
cornerRadius: Dp = 20.dp,
|
||||||
|
iconSize: Dp = 64.dp
|
||||||
) {
|
) {
|
||||||
Box(
|
Box(
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
@ -101,34 +163,192 @@ fun DefaultCoverPlaceholder(
|
|||||||
Icons.Default.MusicNote,
|
Icons.Default.MusicNote,
|
||||||
contentDescription = null,
|
contentDescription = null,
|
||||||
tint = Color.White,
|
tint = Color.White,
|
||||||
modifier = Modifier.size(64.dp)
|
modifier = Modifier.size(iconSize)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class CoverArtwork(
|
private data class AudioCoverModel(
|
||||||
val key: String,
|
val trackId: Long?,
|
||||||
val bitmap: android.graphics.Bitmap
|
val sourcePath: String?,
|
||||||
)
|
val uri: Uri?,
|
||||||
|
val isCoverEdited: Boolean,
|
||||||
|
val cacheToken: Any?
|
||||||
|
) {
|
||||||
|
val cacheKey: String = audioCoverCacheKey(sourcePath, uri, isCoverEdited, cacheToken)
|
||||||
|
}
|
||||||
|
|
||||||
private fun resolveCoverArtwork(
|
private fun audioCoverCacheKey(
|
||||||
context: android.content.Context,
|
|
||||||
sourcePath: String?,
|
sourcePath: String?,
|
||||||
uri: Uri?
|
uri: Uri?,
|
||||||
): CoverArtwork? {
|
isCoverEdited: Boolean,
|
||||||
sourcePath?.let { path ->
|
cacheToken: Any?
|
||||||
findExternalCover(path)?.let { coverPath ->
|
): String {
|
||||||
BitmapFactory.decodeFile(coverPath)?.let { bitmap ->
|
return "audio-cover:${sourcePath ?: uri?.toString().orEmpty()}:edited=$isCoverEdited:${cacheToken ?: ""}"
|
||||||
return CoverArtwork("external:$coverPath", bitmap)
|
}
|
||||||
|
|
||||||
|
private object AudioCoverImageLoader {
|
||||||
|
@Volatile
|
||||||
|
private var instance: ImageLoader? = null
|
||||||
|
|
||||||
|
fun get(context: Context): ImageLoader {
|
||||||
|
return instance ?: synchronized(this) {
|
||||||
|
instance ?: ImageLoader.Builder(context)
|
||||||
|
.components {
|
||||||
|
add(AudioCoverKeyer())
|
||||||
|
add(AudioCoverFetcher.Factory())
|
||||||
|
}
|
||||||
|
.diskCache {
|
||||||
|
DiskCache.Builder()
|
||||||
|
.directory(File(context.cacheDir, COVER_DISK_CACHE_DIR).toOkioPath())
|
||||||
|
.maxSizeBytes(COVER_DISK_CACHE_MAX_BYTES)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
.build()
|
||||||
|
.also { instance = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class AudioCoverKeyer : Keyer<AudioCoverModel> {
|
||||||
|
override fun key(data: AudioCoverModel, options: coil3.request.Options): String {
|
||||||
|
return data.cacheKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class AudioCoverFetcher(
|
||||||
|
private val data: AudioCoverModel,
|
||||||
|
private val options: coil3.request.Options,
|
||||||
|
private val imageLoader: ImageLoader
|
||||||
|
) : Fetcher {
|
||||||
|
override suspend fun fetch(): FetchResult? {
|
||||||
|
if (!data.isCoverEdited) {
|
||||||
|
data.sourcePath?.let { path ->
|
||||||
|
findExternalCover(path)?.let { cover ->
|
||||||
|
coroutineContext.ensureActive()
|
||||||
|
return SourceFetchResult(
|
||||||
|
source = ImageSource(
|
||||||
|
file = cover.toOkioPath(),
|
||||||
|
fileSystem = FileSystem.SYSTEM
|
||||||
|
),
|
||||||
|
mimeType = cover.mimeType(),
|
||||||
|
dataSource = DataSource.DISK
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val uri = data.uri ?: return null
|
||||||
|
val embeddedCover = fetchEmbeddedCover(uri)
|
||||||
|
if (embeddedCover == null && data.isCoverEdited) {
|
||||||
|
markCoverAsNotEdited()
|
||||||
|
}
|
||||||
|
return embeddedCover
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoilApi::class)
|
||||||
|
private suspend fun fetchEmbeddedCover(uri: Uri): FetchResult? {
|
||||||
|
val diskCache = imageLoader.diskCache
|
||||||
|
val cacheKey = data.cacheKey
|
||||||
|
if (options.diskCachePolicy.readEnabled) {
|
||||||
|
diskCache?.openSnapshot(cacheKey)?.let { snapshot ->
|
||||||
|
return snapshot.toSourceResult(diskCache, cacheKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadThumbnailCover(options.context, uri, targetSizePx())?.let { bitmap ->
|
||||||
|
coroutineContext.ensureActive()
|
||||||
|
if (options.diskCachePolicy.writeEnabled && diskCache != null) {
|
||||||
|
writeBitmapToDiskCache(diskCache, cacheKey, bitmap)?.let { snapshot ->
|
||||||
|
return snapshot.toSourceResult(diskCache, cacheKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ImageFetchResult(
|
||||||
|
image = bitmap.asImage(),
|
||||||
|
isSampled = true,
|
||||||
|
dataSource = DataSource.DISK
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!probeEmbeddedCover(options.context, uri)) return null
|
||||||
|
extractEmbeddedCoverWithFfmpeg(options.context, uri, cacheKey, targetSizePx())?.let { file ->
|
||||||
|
coroutineContext.ensureActive()
|
||||||
|
return SourceFetchResult(
|
||||||
|
source = ImageSource(
|
||||||
|
file = file.toOkioPath(),
|
||||||
|
fileSystem = FileSystem.SYSTEM
|
||||||
|
),
|
||||||
|
mimeType = EMBEDDED_COVER_MIME_TYPE,
|
||||||
|
dataSource = DataSource.DISK
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun markCoverAsNotEdited() {
|
||||||
|
val trackId = data.trackId ?: return
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
runCatching {
|
||||||
|
MusicLibraryDatabase.getInstance(options.context.applicationContext)
|
||||||
|
.musicTrackDao()
|
||||||
|
.updateCoverEdited(trackId, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uri?.let { coverUri ->
|
|
||||||
extractEmbeddedCover(context, coverUri)?.let { bitmap ->
|
private fun targetSizePx(): Int {
|
||||||
return CoverArtwork("embedded:$coverUri", bitmap)
|
val width = (options.size.width as? Dimension.Pixels)?.px
|
||||||
|
val height = (options.size.height as? Dimension.Pixels)?.px
|
||||||
|
return maxOf(width ?: 0, height ?: 0).takeIf { it > 0 } ?: DEFAULT_THUMBNAIL_SIZE_PX
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoilApi::class)
|
||||||
|
private fun DiskCache.Snapshot.toSourceResult(
|
||||||
|
diskCache: DiskCache,
|
||||||
|
cacheKey: String
|
||||||
|
): SourceFetchResult {
|
||||||
|
return SourceFetchResult(
|
||||||
|
source = ImageSource(
|
||||||
|
file = this.data,
|
||||||
|
fileSystem = diskCache.fileSystem,
|
||||||
|
diskCacheKey = cacheKey,
|
||||||
|
closeable = this
|
||||||
|
),
|
||||||
|
mimeType = EMBEDDED_COVER_MIME_TYPE,
|
||||||
|
dataSource = DataSource.DISK
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoilApi::class)
|
||||||
|
private fun writeBitmapToDiskCache(
|
||||||
|
diskCache: DiskCache,
|
||||||
|
cacheKey: String,
|
||||||
|
bitmap: Bitmap
|
||||||
|
): DiskCache.Snapshot? {
|
||||||
|
val editor = diskCache.openEditor(cacheKey) ?: return null
|
||||||
|
return try {
|
||||||
|
diskCache.fileSystem.write(editor.metadata) {}
|
||||||
|
diskCache.fileSystem.write(editor.data) {
|
||||||
|
val output = ByteArrayOutputStream()
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, COVER_DISK_CACHE_QUALITY, output)
|
||||||
|
write(output.toByteArray())
|
||||||
|
}
|
||||||
|
editor.commitAndOpenSnapshot()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
editor.abort()
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Factory : Fetcher.Factory<AudioCoverModel> {
|
||||||
|
override fun create(
|
||||||
|
data: AudioCoverModel,
|
||||||
|
options: coil3.request.Options,
|
||||||
|
imageLoader: ImageLoader
|
||||||
|
): Fetcher {
|
||||||
|
return AudioCoverFetcher(data, options, imageLoader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val coverFileNames = listOf(
|
private val coverFileNames = listOf(
|
||||||
@ -137,26 +357,126 @@ private val coverFileNames = listOf(
|
|||||||
"front.jpg", "Front.jpg", "albumart.jpg"
|
"front.jpg", "Front.jpg", "albumart.jpg"
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun findExternalCover(sourcePath: String): String? {
|
private fun findExternalCover(sourcePath: String): File? {
|
||||||
val file = File(sourcePath)
|
val file = File(sourcePath)
|
||||||
val parent = file.parentFile ?: return null
|
val parent = file.parentFile ?: return null
|
||||||
for (name in coverFileNames) {
|
for (name in coverFileNames) {
|
||||||
val cover = File(parent, name)
|
val cover = File(parent, name)
|
||||||
if (cover.exists() && cover.length() > 0) return cover.absolutePath
|
if (cover.exists() && cover.length() > 0) return cover
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun extractEmbeddedCover(context: android.content.Context, uri: Uri): android.graphics.Bitmap? {
|
private fun File.mimeType(): String? {
|
||||||
|
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadThumbnailCover(context: Context, uri: Uri, targetSizePx: Int): Bitmap? {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return null
|
||||||
return try {
|
return try {
|
||||||
val retriever = MediaMetadataRetriever()
|
context.contentResolver.loadThumbnail(
|
||||||
retriever.setDataSource(context, uri)
|
uri,
|
||||||
val picture = retriever.embeddedPicture
|
AndroidSize(targetSizePx, targetSizePx),
|
||||||
retriever.release()
|
null
|
||||||
picture?.let { data ->
|
)
|
||||||
BitmapFactory.decodeByteArray(data, 0, data.size)
|
|
||||||
}
|
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
null
|
null
|
||||||
|
} catch (_: OutOfMemoryError) {
|
||||||
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun probeEmbeddedCover(context: Context, uri: Uri): Boolean {
|
||||||
|
val input = openFfmpegCoverInput(context, uri) ?: return false
|
||||||
|
input.use { handle ->
|
||||||
|
return runCatching {
|
||||||
|
val session = FFprobeKit.executeWithArguments(
|
||||||
|
arrayOf(
|
||||||
|
"-v", "error",
|
||||||
|
"-hide_banner",
|
||||||
|
"-select_streams", "v",
|
||||||
|
"-show_entries", "stream=index,codec_type,codec_name,width,height:stream_disposition=attached_pic",
|
||||||
|
"-of", "json",
|
||||||
|
handle.path
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (session.returnCode?.isValueSuccess != true) return false
|
||||||
|
JSONObject(session.output.orEmpty())
|
||||||
|
.optJSONArray("streams")
|
||||||
|
?.hasEmbeddedCoverStream() == true
|
||||||
|
}.getOrDefault(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun org.json.JSONArray.hasEmbeddedCoverStream(): Boolean {
|
||||||
|
for (index in 0 until length()) {
|
||||||
|
val stream = optJSONObject(index) ?: continue
|
||||||
|
if (stream.optString("codec_type") != "video") continue
|
||||||
|
val disposition = stream.optJSONObject("disposition")
|
||||||
|
if (disposition?.optInt("attached_pic", 0) == 1) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun extractEmbeddedCoverWithFfmpeg(
|
||||||
|
context: Context,
|
||||||
|
uri: Uri,
|
||||||
|
cacheKey: String,
|
||||||
|
targetSizePx: Int
|
||||||
|
): File? {
|
||||||
|
val input = openFfmpegCoverInput(context, uri) ?: return null
|
||||||
|
input.use { handle ->
|
||||||
|
val output = File(context.cacheDir, COVER_FFMPEG_CACHE_DIR).apply { mkdirs() }
|
||||||
|
.resolve("${cacheKey.safeFileName()}.jpg")
|
||||||
|
val session = FFmpegKit.executeWithArguments(
|
||||||
|
arrayOf(
|
||||||
|
"-v", "error",
|
||||||
|
"-hide_banner",
|
||||||
|
"-y",
|
||||||
|
"-i", handle.path,
|
||||||
|
"-map", "0:v:0",
|
||||||
|
"-frames:v", "1",
|
||||||
|
"-vf", "scale=${targetSizePx}:${targetSizePx}:force_original_aspect_ratio=decrease",
|
||||||
|
output.absolutePath
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return output.takeIf {
|
||||||
|
session.returnCode?.isValueSuccess == true && it.exists() && it.length() > 0L
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openFfmpegCoverInput(context: Context, uri: Uri): FfmpegCoverInput? {
|
||||||
|
return when (uri.scheme) {
|
||||||
|
"file" -> FfmpegCoverInput(uri.path.orEmpty())
|
||||||
|
"content" -> {
|
||||||
|
val descriptor = context.contentResolver.openFileDescriptor(uri, "r") ?: return null
|
||||||
|
FfmpegCoverInput(
|
||||||
|
path = "/proc/self/fd/${descriptor.fd}",
|
||||||
|
closeable = descriptor
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else -> FfmpegCoverInput(FFmpegKitConfig.getSafParameterForRead(context, uri))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class FfmpegCoverInput(
|
||||||
|
val path: String,
|
||||||
|
private val closeable: AutoCloseable? = null
|
||||||
|
) : AutoCloseable {
|
||||||
|
override fun close() {
|
||||||
|
closeable?.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun String.safeFileName(): String {
|
||||||
|
return replace(Regex("[^A-Za-z0-9._-]"), "_").take(160)
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val COVER_CROSSFADE_MS = 260
|
||||||
|
private const val COVER_DISK_CACHE_DIR = "audio_vibe_cover_cache"
|
||||||
|
private const val COVER_FFMPEG_CACHE_DIR = "audio_vibe_cover_ffmpeg"
|
||||||
|
private const val COVER_DISK_CACHE_MAX_BYTES = 96L * 1024L * 1024L
|
||||||
|
private const val COVER_DISK_CACHE_QUALITY = 88
|
||||||
|
private const val DEFAULT_THUMBNAIL_SIZE_PX = 512
|
||||||
|
private const val EMBEDDED_COVER_MIME_TYPE = "image/*"
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.Box
|
|||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
@ -17,7 +18,6 @@ import androidx.compose.foundation.layout.size
|
|||||||
import androidx.compose.foundation.layout.width
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.MusicNote
|
|
||||||
import androidx.compose.material.icons.filled.Pause
|
import androidx.compose.material.icons.filled.Pause
|
||||||
import androidx.compose.material.icons.filled.PlayArrow
|
import androidx.compose.material.icons.filled.PlayArrow
|
||||||
import androidx.compose.material.icons.filled.SkipNext
|
import androidx.compose.material.icons.filled.SkipNext
|
||||||
@ -77,14 +77,17 @@ fun MiniPlayerBar(
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(44.dp)
|
.size(44.dp)
|
||||||
.clip(RoundedCornerShape(10.dp))
|
.clip(RoundedCornerShape(10.dp))
|
||||||
.background(AudioVibeColors.PrimarySoft),
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
) {
|
||||||
Icon(
|
CoverImage(
|
||||||
Icons.Default.MusicNote,
|
trackId = state.currentId,
|
||||||
contentDescription = null,
|
sourcePath = state.sourcePath,
|
||||||
tint = AudioVibeColors.Primary,
|
uri = state.uri,
|
||||||
modifier = Modifier.size(22.dp)
|
isCoverEdited = state.isCoverEdited,
|
||||||
|
cornerRadius = 10.dp,
|
||||||
|
placeholderIconSize = 22.dp,
|
||||||
|
decodeSize = 56.dp,
|
||||||
|
animateChanges = false,
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.audio.vibe.ui.player
|
package com.audio.vibe.ui.player
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import com.audio.vibe.data.playback.PlaybackState
|
import com.audio.vibe.data.playback.PlaybackState
|
||||||
|
|
||||||
@ -8,6 +9,9 @@ data class MiniPlayerState(
|
|||||||
val currentId: Long? = null,
|
val currentId: Long? = null,
|
||||||
val title: String = "",
|
val title: String = "",
|
||||||
val artist: String = "",
|
val artist: String = "",
|
||||||
|
val sourcePath: String? = null,
|
||||||
|
val uri: Uri? = null,
|
||||||
|
val isCoverEdited: Boolean = false,
|
||||||
val isPlaying: Boolean = false,
|
val isPlaying: Boolean = false,
|
||||||
val progressFraction: Float = 0f,
|
val progressFraction: Float = 0f,
|
||||||
val errorMessage: String? = null
|
val errorMessage: String? = null
|
||||||
@ -27,6 +31,9 @@ data class MiniPlayerState(
|
|||||||
currentId = state.currentId,
|
currentId = state.currentId,
|
||||||
title = track?.title ?: state.currentTitle.orEmpty(),
|
title = track?.title ?: state.currentTitle.orEmpty(),
|
||||||
artist = track?.artist ?: state.currentArtist.orEmpty(),
|
artist = track?.artist ?: state.currentArtist.orEmpty(),
|
||||||
|
sourcePath = track?.sourcePath,
|
||||||
|
uri = track?.uri,
|
||||||
|
isCoverEdited = track?.isCoverEdited == true,
|
||||||
isPlaying = state.isPlaying,
|
isPlaying = state.isPlaying,
|
||||||
progressFraction = if (state.durationMs != null && state.durationMs > 0) {
|
progressFraction = if (state.durationMs != null && state.durationMs > 0) {
|
||||||
(state.progressMs.toFloat() / state.durationMs).coerceIn(0f, 1f)
|
(state.progressMs.toFloat() / state.durationMs).coerceIn(0f, 1f)
|
||||||
|
|||||||
@ -133,8 +133,10 @@ private fun PlayerContent(
|
|||||||
|
|
||||||
Box(modifier = Modifier.fillMaxSize()) {
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
PlayerBackdrop(
|
PlayerBackdrop(
|
||||||
|
trackId = uiState.currentId,
|
||||||
sourcePath = uiState.sourcePath,
|
sourcePath = uiState.sourcePath,
|
||||||
uri = uiState.uri,
|
uri = uiState.uri,
|
||||||
|
isCoverEdited = uiState.isCoverEdited,
|
||||||
modifier = Modifier.fillMaxSize()
|
modifier = Modifier.fillMaxSize()
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -154,8 +156,10 @@ private fun PlayerContent(
|
|||||||
Spacer(Modifier.weight(1f))
|
Spacer(Modifier.weight(1f))
|
||||||
|
|
||||||
CoverImage(
|
CoverImage(
|
||||||
|
trackId = uiState.currentId,
|
||||||
sourcePath = uiState.sourcePath,
|
sourcePath = uiState.sourcePath,
|
||||||
uri = uiState.uri,
|
uri = uiState.uri,
|
||||||
|
isCoverEdited = uiState.isCoverEdited,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth(0.82f)
|
.fillMaxWidth(0.82f)
|
||||||
.widthIn(max = 320.dp)
|
.widthIn(max = 320.dp)
|
||||||
@ -359,8 +363,10 @@ private fun MarqueeText(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun PlayerBackdrop(
|
private fun PlayerBackdrop(
|
||||||
|
trackId: Long?,
|
||||||
sourcePath: String?,
|
sourcePath: String?,
|
||||||
uri: android.net.Uri?,
|
uri: android.net.Uri?,
|
||||||
|
isCoverEdited: Boolean,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
) {
|
) {
|
||||||
Box(modifier = modifier) {
|
Box(modifier = modifier) {
|
||||||
@ -383,8 +389,10 @@ private fun PlayerBackdrop(
|
|||||||
.blur(64.dp)
|
.blur(64.dp)
|
||||||
) {
|
) {
|
||||||
CoverImage(
|
CoverImage(
|
||||||
|
trackId = trackId,
|
||||||
sourcePath = sourcePath,
|
sourcePath = sourcePath,
|
||||||
uri = uri,
|
uri = uri,
|
||||||
|
isCoverEdited = isCoverEdited,
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
cornerRadius = 0.dp
|
cornerRadius = 0.dp
|
||||||
)
|
)
|
||||||
|
|||||||
@ -29,6 +29,7 @@ data class PlayerUiState(
|
|||||||
val errorMessage: String? = null,
|
val errorMessage: String? = null,
|
||||||
val sourcePath: String? = null,
|
val sourcePath: String? = null,
|
||||||
val uri: android.net.Uri? = null,
|
val uri: android.net.Uri? = null,
|
||||||
|
val isCoverEdited: Boolean = false,
|
||||||
val showBottomSheet: Boolean = false,
|
val showBottomSheet: Boolean = false,
|
||||||
val showQueueSheet: Boolean = false
|
val showQueueSheet: Boolean = false
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -104,6 +104,7 @@ private fun PlayerUiState.fromPlaybackState(state: PlaybackState): PlayerUiState
|
|||||||
outputParameters = if (hasCurrentTrack) state.outputParameters ?: outputParameters else null,
|
outputParameters = if (hasCurrentTrack) state.outputParameters ?: outputParameters else null,
|
||||||
errorMessage = state.errorMessage,
|
errorMessage = state.errorMessage,
|
||||||
sourcePath = state.currentTrack?.sourcePath,
|
sourcePath = state.currentTrack?.sourcePath,
|
||||||
uri = state.currentTrack?.uri
|
uri = state.currentTrack?.uri,
|
||||||
|
isCoverEdited = state.currentTrack?.isCoverEdited == true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ jaudiotagger = "3.0.1"
|
|||||||
ffmpegKit = "6.0.1"
|
ffmpegKit = "6.0.1"
|
||||||
media3 = "1.10.0"
|
media3 = "1.10.0"
|
||||||
timber = "5.0.1"
|
timber = "5.0.1"
|
||||||
coil = "2.7.0"
|
coil = "3.4.0"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||||
@ -59,7 +59,7 @@ jaudiotagger = { group = "net.jthink", name = "jaudiotagger", version.ref = "jau
|
|||||||
ffmpeg-kit-min = { group = "io.github.maitrungduc1410", name = "ffmpeg-kit-min", version.ref = "ffmpegKit" }
|
ffmpeg-kit-min = { group = "io.github.maitrungduc1410", name = "ffmpeg-kit-min", version.ref = "ffmpegKit" }
|
||||||
androidx-media3-exoplayer = { group = "androidx.media3", name = "media3-exoplayer", version.ref = "media3" }
|
androidx-media3-exoplayer = { group = "androidx.media3", name = "media3-exoplayer", version.ref = "media3" }
|
||||||
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
|
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
|
||||||
coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
|
coil-compose = { group = "io.coil-kt.coil3", name = "coil-compose", version.ref = "coil" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user