Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/src/main/java/com/zionhuang/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ class MainActivity : ComponentActivity() {
super.onDestroy()
if (dataStore.get(StopMusicOnTaskClearKey, false) && playerConnection?.isPlaying?.value == true && isFinishing) {
stopService(Intent(this, MusicService::class.java))
unbindService(serviceConnection)
playerConnection = null
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/zionhuang/music/lyrics/LyricsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LyricsHelper @Inject constructor(
mediaMetadata.artists.joinToString { it.name },
mediaMetadata.duration
).onSuccess { lyrics ->
cache.put(mediaMetadata.id, listOf(LyricsResult(provider.name, lyrics)))
return lyrics
}.onFailure {
reportException(it)
Expand All @@ -43,8 +44,7 @@ class LyricsHelper @Inject constructor(
duration: Int,
callback: (LyricsResult) -> Unit,
) {
val cacheKey = "$songArtists-$songTitle".replace(" ", "")
cache.get(cacheKey)?.let { results ->
cache.get(mediaId)?.let { results ->
results.forEach {
callback(it)
}
Expand All @@ -60,7 +60,7 @@ class LyricsHelper @Inject constructor(
}
}
}
cache.put(cacheKey, allResult)
cache.put(mediaId, allResult)
}

companion object {
Expand Down
40 changes: 24 additions & 16 deletions app/src/main/java/com/zionhuang/music/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class MusicService : MediaLibraryService(),
return@Factory dataSpec
}

songUrlCache[mediaId]?.takeIf { it.second < System.currentTimeMillis() }?.let {
songUrlCache[mediaId]?.takeIf { it.second > System.currentTimeMillis() }?.let {
scope.launch(Dispatchers.IO) { recoverSong(mediaId) }
return@Factory dataSpec.withUri(it.first.toUri())
}
Expand Down Expand Up @@ -675,24 +675,32 @@ class MusicService : MediaLibraryService(),
}
} ?: throw PlaybackException(getString(R.string.error_no_stream), null, ERROR_CODE_NO_STREAM)

database.query {
upsert(
FormatEntity(
id = mediaId,
itag = format.itag,
mimeType = format.mimeType.split(";")[0],
codecs = format.mimeType.split("codecs=")[1].removeSurrounding("\""),
bitrate = format.bitrate,
sampleRate = format.audioSampleRate,
contentLength = format.contentLength!!,
loudnessDb = playerResponse.playerConfig?.audioConfig?.loudnessDb
)
)
val streamUrl = format.url
?: throw PlaybackException(getString(R.string.error_no_stream), null, ERROR_CODE_NO_STREAM)

if (format.contentLength != null) {
val codecs = format.mimeType.split("codecs=").getOrNull(1)?.removeSurrounding("\"")
if (codecs != null) {
database.query {
upsert(
FormatEntity(
id = mediaId,
itag = format.itag,
mimeType = format.mimeType.split(";")[0],
codecs = codecs,
bitrate = format.bitrate,
sampleRate = format.audioSampleRate,
contentLength = format.contentLength,
loudnessDb = playerResponse.playerConfig?.audioConfig?.loudnessDb
)
)
}
}
}
scope.launch(Dispatchers.IO) { recoverSong(mediaId, playerResponse) }

songUrlCache[mediaId] = format.url!! to playerResponse.streamingData!!.expiresInSeconds * 1000L
dataSpec.withUri(format.url!!.toUri()).subrange(dataSpec.uriPositionOffset, CHUNK_LENGTH)
songUrlCache[mediaId] = streamUrl to System.currentTimeMillis() + playerResponse.streamingData!!.expiresInSeconds * 1000L
dataSpec.withUri(streamUrl.toUri()).subrange(dataSpec.uriPositionOffset, CHUNK_LENGTH)
}
}

Expand Down