Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A source picked in the player while the song is paused leaves it paused: an extension picked over Spotify puts the song there from where it was, paused (the page stays paused, the Connect mirror starts only with the play), Spotify picked over an extension keeps it paused where it is and the next play plays it on Spotify from there (unless another song came meanwhile or "Back to my order" put an extension first again), another extension reopens it paused as before; the song starts only at the user's play [skip ci]
kota-zoc1cd82ce8e91
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 50eafd40..858fe5f8 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@@ -365,9 +365,10 @@ object LocalPlayer {/*** [startMs]: where the song at [index] starts (a resume). [continues]: the extensions play the- * queue, it goes on after its last song as Spotify's does (see continueIfLast).+ * queue, it goes on after its last song as Spotify's does (see continueIfLast). [paused]: the song+ * is put there paused (another source picked for it while it was paused), the user's play starts it.*/- fun play(list: List<LocalTrack>, index: Int, shuffle: Boolean = false, context: String? = null, contextUri: String = "", startMs: Long = 0L, continues: Boolean = false) {+ fun play(list: List<LocalTrack>, index: Int, shuffle: Boolean = false, context: String? = null, contextUri: String = "", startMs: Long = 0L, continues: Boolean = false, paused: Boolean = false) {if (list.isEmpty()) return// Another song picked: repeat once was for the song left.val shown = SongPlayer.nowPlaying.value.uri.substringAfterLast(':')@@ -395,10 +396,12 @@ object LocalPlayer {replaying = nullrepeat = SongPlayer.repeatMode.valueapplyRepeat(p)+ // Paused before it loads: a player left playing by a song before never starts this one.+ if (paused) p.pause()p.setMediaItems(all.map(::item), index.coerceIn(0, list.lastIndex), startMs.coerceAtLeast(0L))p.prepare()if (shuffle) setShuffle(true)- p.play()+ if (!paused) p.play()continueIfLast(p)}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 0be4175f..b6f2f22f 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@@ -1208,10 +1208,13 @@ object SongPlayer {* saved) and the song playing plays from it, from where it is. An extension is asked for the* song alone first, nothing stopping meanwhile: without it ([SourceSwitch.MISSING]) the song* goes on and the order stays as it was. Spotify picked while an extension streams: Spotify- * plays the song from there, in its list. [done] on the main thread.+ * plays the song from there, in its list. Picked while paused, the song stays paused: put on+ * the new source where it was, it starts at the user's next play ([putPausedOnExtension],+ * [spotifyDue]). [done] on the main thread.*/fun switchSource(id: String, done: (SourceSwitch) -> Unit) {val ext = com.music.spotui.extensions.Extensions+ spotifyDue = nullval local = LocalPlayer.active.valueval t = if (local) LocalPlayer.current() else nullval streamed = t?.let(::streamedTrackId).orEmpty()@@ -1222,6 +1225,12 @@ object SongPlayer {done(when {!local -> SourceSwitch.PLAYINGt == null || streamed.isBlank() || elsewhere -> SourceSwitch.NEXT_SONG+ // Paused: it stays paused where it is, the user's next play plays it on Spotify from there.+ !LocalPlayer.playWhenReady() -> {+ spotifyDue = streamed+ app.myspoti.host.android.Logbook.log("local: ${t.title} paused, Spotify plays it at the next play")+ SourceSwitch.PLAYING+ }playLocalOnSpotify(t, LocalPlayer.position(), "picked in the player") -> SourceSwitch.PLAYINGelse -> SourceSwitch.NEXT_SONG})@@ -1252,12 +1261,50 @@ object SongPlayer {val np = _nowPlaying.valueval q = _queue.valueval songs = (listOfNotNull(q.current) + (q.queued + q.nextUp).map { it.song }).takeIf { l -> l.any { it.spotifyTrackId == trackId } }+ if (!isPlaying()) { putPausedOnExtension(trackId, songs ?: listOf(song), done); return@post }playSong(np.uri, contextUri = np.contextUri.ifBlank { null }, positionMs = getCurrentPosition(), songs = songs)done(SourceSwitch.PLAYING)}}}+ /**+ * An extension picked in the player while Spotify had the song [trackId] paused, its answer kept+ * (ExtensionAudio.use): the local player takes it from where Spotify was, with [songs] as its+ * queue (as ExtensionAudio.playList does), paused; nothing plays until the user's play (the page+ * stays paused, the Connect mirror starts only with it). At once, on the main thread: the+ * sources changing next keep the answer of the song the local player is on.+ */+ private fun putPausedOnExtension(trackId: String, songs: List<SongsModel>, done: (SourceSwitch) -> Unit) {+ if (_activeDevice.value != null) { done(SourceSwitch.NEXT_SONG); return }+ val np = _nowPlaying.value+ val picked = songs.first { it.spotifyTrackId == trackId }+ val ctx = np.contextUri.ifBlank { contextRegistry[np.uri].orEmpty() }+ val at = getCurrentPosition()+ app.myspoti.host.android.Logbook.log("audio: ${picked.title} paused, put on the extension at $at ms, still paused")+ val (list, index) = com.music.spotui.extensions.AudioRoute.queue(songs, picked)+ LocalPlayer.play(list.map(com.music.spotui.extensions.ExtensionAudio::localTrack), index, shuffle = isShuffleOn(), context = knownContextName(ctx),+ contextUri = ctx, startMs = at, continues = true, paused = true)+ done(SourceSwitch.PLAYING)+ }++ /**+ * The song an extension streams that Spotify was picked for while it was paused (see+ * [switchSource]): the user's next play plays it on Spotify from where it is, the local player+ * stays on it paused until then. Null: none.+ */+ private var spotifyDue: String? = null++ /** The user's play on the song [spotifyDue] names: Spotify plays it from where it is. False: the local player resumes as usual. */+ private fun playDueOnSpotify(): Boolean {+ val due = spotifyDue.also { spotifyDue = null } ?: return false+ val t = LocalPlayer.current() ?: return false+ val first = com.music.spotui.extensions.Extensions.sourceLine(app.myspoti.extensions.Action.AUDIO_SOURCE).firstOrNull()+ if (!com.music.spotui.extensions.SessionSource.spotifyDue(due, streamedTrackId(t), first)) return false+ if (_activeDevice.value != null || routing || com.music.spotui.cast.CastPlayback.active) return false+ return playLocalOnSpotify(t, LocalPlayer.position(), "picked in the player while paused")+ }+/*** The local queue the extensions played ended with no song radio to go on with: Spotify goes* on after its last song [t] ([trackId]) as after a song played alone (its autoplay), from the@@ -1426,7 +1473,7 @@ object SongPlayer {fun play(by: String = "user") {app.myspoti.host.android.Logbook.log("play asked by $by")if (routing) return // the song asked for starts by itself- if (LocalPlayer.active.value) { Haptics.click(); LocalPlayer.resume(); return }+ if (LocalPlayer.active.value) { Haptics.click(); if (!playDueOnSpotify()) LocalPlayer.resume(); return }if (resumeRestoredLocal()) { Haptics.click(); return }if (resumeThroughExtensions()) { Haptics.click(); return }// Smart shuffle was turned off while paused: the list's order comes back as it plays on.@@ -2760,6 +2807,8 @@ object SongPlayer {// A download keeps its Spotify id too (likes, menu): only a file of the device has none.currentTrackId = trackId.ifBlank { localSpotifyUri(t)?.let(::trackIdOf).orEmpty() }playing = isPlaying; positionMs = pos; positionAt = SystemClock.elapsedRealtime(); durationMs = dur+ // Played on here (another device's resume through the Connect mirror): Spotify no longer waits for a play.+ if (isPlaying) spotifyDue = nullbufferingState.value = false// A song an extension streams keeps its Spotify uri: its lyrics, Canvas and artists load as for Spotify.val uri = if (trackId.isNotBlank()) SPOTIFY_TRACK_PREFIX + trackId else "local:${t.id}"diff --git a/base/app/src/main/java/com/music/spotui/extensions/SessionSource.kt b/base/app/src/main/java/com/music/spotui/extensions/SessionSource.ktindex f2534809..02219897 100644--- a/base/app/src/main/java/com/music/spotui/extensions/SessionSource.kt+++ b/base/app/src/main/java/com/music/spotui/extensions/SessionSource.kt@@ -23,6 +23,15 @@ object SessionSource {fun pick(id: String, saved: List<String>, enabled: (String) -> Boolean): String? =valid(id, saved, enabled)?.takeIf { it != SectionSettings.leader(saved, enabled) }+ /**+ * Spotify was picked while the song an extension streams, [dueId], was paused: it stayed paused+ * there, and the user's next play plays it on Spotify from where it is, as long as the local+ * player is still on it ([currentId]) and Spotify is still the first source asked ([first], the+ * order asked now: "Back to my order" meanwhile keeps the song where it is).+ */+ fun spotifyDue(dueId: String?, currentId: String?, first: String?): Boolean =+ dueId != null && dueId.isNotBlank() && dueId == currentId && first == SPOTIFY_SOURCE+/** What the quality sheet offers: Spotify and the extensions on, in the [effective] order. */fun choices(saved: List<String>, lead: String?, enabled: (String) -> Boolean): List<String> =effective(saved, valid(lead, saved, enabled)).filter { it == SPOTIFY_SOURCE || enabled(it) }diff --git a/base/app/src/test/java/com/music/spotui/extensions/SessionSourceTest.kt b/base/app/src/test/java/com/music/spotui/extensions/SessionSourceTest.ktindex 7436e149..9af26298 100644--- a/base/app/src/test/java/com/music/spotui/extensions/SessionSourceTest.kt+++ b/base/app/src/test/java/com/music/spotui/extensions/SessionSourceTest.kt@@ -1,7 +1,9 @@package com.music.spotui.extensionsimport org.junit.Assert.assertEquals+import org.junit.Assert.assertFalseimport org.junit.Assert.assertNull+import org.junit.Assert.assertTrueimport org.junit.Test/** The audio source picked in the player for the session: first, then the user's order; gone with the source. */@@ -54,4 +56,16 @@ class SessionSourceTest {// The pick turned off meanwhile: the user's order, without it.assertEquals(listOf(a, SPOTIFY_SOURCE, c), SessionSource.choices(saved, b, { it != b }))}++ @Test fun spotifyPickedWhilePausedPlaysTheSongThereAtTheNextPlayOnly() {+ // Still on that song, Spotify still first: the next play plays it on Spotify.+ assertTrue(SessionSource.spotifyDue("4uLU6hMCjMI75M1A2tKUQC", "4uLU6hMCjMI75M1A2tKUQC", SPOTIFY_SOURCE))+ // Nothing picked while paused, or a song with no Spotify id.+ assertFalse(SessionSource.spotifyDue(null, "4uLU6hMCjMI75M1A2tKUQC", SPOTIFY_SOURCE))+ assertFalse(SessionSource.spotifyDue("", "", SPOTIFY_SOURCE))+ // Another song meanwhile (a next, a pick): it plays where it is.+ assertFalse(SessionSource.spotifyDue("4uLU6hMCjMI75M1A2tKUQC", "7ouMYWpwJ422jRcDASZB7P", SPOTIFY_SOURCE))+ // "Back to my order" meanwhile, an extension first again: the song stays on it.+ assertFalse(SessionSource.spotifyDue("4uLU6hMCjMI75M1A2tKUQC", "4uLU6hMCjMI75M1A2tKUQC", a))+ }}