Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Lyrics with extension songs: the words hold while the audio loads after a tap or at a song's start instead of running ahead, and a lyric tap logs where the player really went (source, seekable, position after 300 ms and 2 s), with a note when the audio's length differs from the song's
kota-zo6d94f87c22ee
diff --git a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.ktindex 0558fb26..363a18cd 100644--- a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt@@ -81,6 +81,7 @@ object LocalPlayer {log("state ${stateName(state)} at ${p.currentPosition} ms, item ${p.currentMediaItemIndex + 1}/${p.mediaItemCount}")// Casting: this player keeps the queue only, stopped; the song plays on the device.if (CastPlayback.active && state != Player.STATE_IDLE) { p.stop(); return }+ if (state == Player.STATE_READY) checkLength(p)if (state == Player.STATE_ENDED) atEnd(p)}override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {@@ -609,6 +610,47 @@ object LocalPlayer {fun duration() = exo?.duration?.takeIf { it > 0 } ?: current()?.durationMs ?: 0Lfun current(): LocalTrack? = exo?.currentMediaItemIndex?.let { tracks.getOrNull(it) }+ /**+ * For the diagnostic: what plays (an extension and how it streams, a download, a file), whether+ * its source can seek (a source that cannot plays a seek from the start), its state, and its+ * length against the song's (Spotify's, for a stream or a download).+ */+ fun describe(): String {+ val t = current() ?: return "nothing"+ val p = exo ?: return "no player"+ val id = t.uri.lastPathSegment.orEmpty()+ val what = when {+ t.uri.scheme == com.music.spotui.extensions.ExtensionAudio.SCHEME ->+ "extension ${com.music.spotui.extensions.ExtensionAudio.providerOf(id) ?: "?"} (${com.music.spotui.extensions.ExtensionAudio.deliveryOf(id) ?: "not answered"})"+ com.music.spotui.data.offline.OfflineStore.byLocalId(t.id) != null -> "download"+ else -> "file"+ }+ val seek = when {+ CastPlayback.active -> "casting"+ p.isCurrentMediaItemSeekable -> "seekable"+ else -> "NOT seekable"+ }+ return "$what, $seek, ${playbackState()}, ${p.duration.takeIf { it > 0 } ?: "unknown"} ms long (song ${t.durationMs} ms)"+ }++ /** The song whose length was compared with its audio's (see [checkLength]). */+ private var lengthChecked: LocalTrack? = null++ /**+ * Once per song, when its audio is ready: an audio longer or shorter than the song (another+ * cut, an intro) puts synced lyrics, timed on Spotify's, off by the difference. Logged only.+ */+ private fun checkLength(p: ExoPlayer) {+ val t = current() ?: return+ if (lengthChecked === t) return+ val audio = p.duration.takeIf { it > 0 } ?: return+ lengthChecked = t+ if (t.durationMs > 0 && kotlin.math.abs(audio - t.durationMs) > LENGTH_GAP_MS)+ log("${t.title}: the audio lasts $audio ms, the song ${t.durationMs} ms (synced lyrics may be off by the difference)")+ }++ private const val LENGTH_GAP_MS = 2_000L+private fun log(line: String) = app.myspoti.host.android.Logbook.log("local: $line")/** An error's text without the addresses it may carry. */diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.ktindex 47ceb4ec..14e26b8e 100644--- a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt@@ -2327,6 +2327,33 @@ object SongPlayer {return if (durationMs > 0) now.coerceAtMost(durationMs) else now}+ /**+ * Whether [getCurrentPosition] moves on now. Not only paused: a song that is to play but is+ * loading (its start, a seek into what is not loaded yet, a stream that does not keep up)+ * stands still too, for seconds with an extension's stream; the lyrics hold meanwhile.+ */+ fun positionMoving(): Boolean =+ if (LocalPlayer.active.value && !routing) LocalPlayer.isPlaying() else playing && !isBuffering++ /**+ * A tap on a synced lyric line, for the diagnostic: where it jumps, which player plays (and+ * whether its source can seek), then where the song is 300 ms and 2 s later.+ */+ fun logLyricJump(targetMs: Long) {+ val log = app.myspoti.host.android.Logbook::log+ val player = when {+ routing -> "spotify, an extension being asked"+ LocalPlayer.active.value -> "local, ${LocalPlayer.describe()}"+ _activeDevice.value != null -> "spotify connect"+ else -> "spotify"+ }+ log("lyrics: tap on the line at $targetMs ms, from ${getCurrentPosition()} ms ($player)")+ for (afterMs in longArrayOf(300L, 2_000L)) main.postDelayed({+ log("lyrics: $afterMs ms after the tap to $targetMs ms: at ${getCurrentPosition()} ms" ++ if (positionMoving()) "" else if (isPlaying() || LocalPlayer.active.value && LocalPlayer.playWhenReady()) ", loading" else ", paused")+ }, afterMs)+ }+/** A local file starts: Spotify pauses (wherever it plays). */fun pauseEngineForLocal() {// A song the extensions were asked for has its answer, or other files play: older answers play nothing.diff --git a/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt b/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.ktindex a58e5c43..5e01f2e8 100644--- a/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt+++ b/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt@@ -112,6 +112,10 @@ object ExtensionAudio {/** The quality the extension announced for the song [trackId] ("FLAC · 24-bit · 96 kHz"), or null. */fun qualityOf(trackId: String): String? = known[trackId]?.stream?.qualityLabel()+ /** How the audio of the song [trackId] comes, for the diagnostic ("dash", "url audio/flac"), or null. */+ fun deliveryOf(trackId: String): String? =+ known[trackId]?.stream?.let { if (it.manifest != null) "dash" else "url ${it.mimeType ?: "untyped"}" }+/*** For the player's data source: the audio of a `myspoti-ext:` item, or null. Blocks.* [again]: the answer kept is dropped and the extensions are asked anew (its address was refused).diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricClock.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricClock.ktindex 64ed74a6..942dde96 100644--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricClock.kt+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricClock.kt@@ -93,6 +93,18 @@ internal fun rememberLyricsPlayhead(trackKey: Any, playing: Boolean): LyricsPlayval system = System.nanoTime()val now = nanosToMs(if (abs(system - frameNanos) < SAME_CLOCK_NANOS) frameNanos else system)val position = playerPosition()+ // To play but loading (a seek into a stream, its start): the position stands+ // still, and so do the words. Carried on at the learned rate, they ran ahead+ // of the song by the whole wait (seconds with an extension's stream) and then+ // crept back at half speed: a tapped line seemed not to jump, then the words+ // were out of step. The next frame that moves starts a fresh run from there.+ if (!SongPlayer.positionMoving()) {+ head.engine.hold(position, now)+ head.clock.longValue = position+ head.lastReadMs = position+ head.lastReadAtMs = Double.NaN+ return@withFrameNanos+ }if (!head.lastReadAtMs.isNaN() &&abs(position - (head.lastReadMs + (now - head.lastReadAtMs))) > SEEK_JUMP_MS) {diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.ktindex 1371bd48..d25a6f59 100644--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt@@ -447,6 +447,7 @@ private fun LyricsList(}},onSeek = { timeMs ->+ SongPlayer.logLyricJump(timeMs)head.announceJump()SongPlayer.seekTo(timeMs)SongPlayer.play()diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.ktindex a09c0271..ef01d663 100644--- a/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt+++ b/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt@@ -196,6 +196,7 @@ private fun sungChars(words: List<LyricWord>, starts: List<Int>, p: Long): Float/** Seek to a tapped synced line and make sure we're playing. */private fun jumpTo(timeMs: Long) {com.music.spotui.di.Haptics.click()+ SongPlayer.logLyricJump(timeMs)SongPlayer.seekTo(timeMs)if (!SongPlayer.isPlaying()) SongPlayer.play()}