Skip to content

Native Android client for Spotify, built with Jetpack Compose

Playback no longer stands still when a song is on no extension and Spotify refuses it: the refusal steps reset only once the song asked plays (a Jam host's song reset them for minutes), and a song sent to Spotify for want of the extensions that does not start within 10 s is skipped, the local queue going on

kota-zo5b73a83ea469
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
index dd4a8ce9..2dd86e44 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
@@ -1580,9 +1580,13 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
}
adsOffSent = false
adMuteTries = 0
- // A real song is playing: the fallback after a refusal (play the list without picking the
- // song) is armed again for the next command, not held once per five seconds for all of them.
- refusalStep = 0
+ // The song asked is playing (or the ask is long gone): the fallback after a refusal (play the
+ // list without picking the song) is armed again for the next command. Another song (the one
+ // left playing, the Jam host's) does not count: seen with a play refused in a Jam, steps 1
+ // and 2 went on in turns for minutes, each state of the old song resetting them.
+ val asked = lastPlay
+ if (asked == null || System.currentTimeMillis() - asked.third > LATE_REFUSED_WINDOW_MS ||
+ (if (asked.second != null) s?.item?.uri == asked.second else s?.contextUri == asked.first)) refusalStep = 0
if (s?.item != null) lastSong = s
else if (lastTrackState?.item != null) lastSongUntil = System.currentTimeMillis()
// Another song: the stall was the last one's (the watcher judges this one on its next tick).
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.kt
index c44339ef..4fdc547f 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
@@ -1270,9 +1270,58 @@ object SongPlayer {
fallbackRest = Triple(id, LocalPlayer.upcoming().map { (_, u) -> localSong(u).copy(spotifyTrackId = LocalPlayer.spotifyIdOf(u)) }, LocalPlayer.contextName)
playSong(buildSpotifyPlayQuery(id, t.title, t.artist), contextUri = LocalPlayer.contextUri.ifBlank { null },
positionMs = positionMs.takeIf { it > 0 }, askExtensions = false)
+ watchFallback(id, t.title)
return true
}
+ /**
+ * A song of the local queue sent to Spotify for want of the extensions ([playLocalOnSpotify])
+ * must be playing there within [FALLBACK_START_MS]; else (Spotify refused it: a Jam, a free
+ * account's limits, no network to it) the queue goes on with its next song, as a song neither
+ * has is skipped, rather than standing still on a refused play.
+ */
+ private fun watchFallback(id: String, title: String) {
+ fallbackWatch?.cancel()
+ fallbackWatch = scope.launch {
+ kotlinx.coroutines.delay(FALLBACK_START_MS)
+ main.post {
+ if (spotifyFallbackId != id || LocalPlayer.active.value) return@post
+ if (playing && trackIdOf(_nowPlaying.value.uri) == id) { fallbackFailures = 0; return@post }
+ fallbackFailed(id, title)
+ }
+ }
+ }
+
+ private var fallbackWatch: kotlinx.coroutines.Job? = null
+ /** Songs skipped in a row because neither the extensions nor Spotify played them. */
+ private var fallbackFailures = 0
+
+ private fun fallbackFailed(id: String, title: String) {
+ val rest = fallbackRest?.takeIf { it.first == id }
+ val songs = rest?.second?.filter { it.spotifyTrackId.isNotBlank() && it.spotifyTrackId != id }.orEmpty()
+ val next = songs.firstOrNull()
+ fallbackFailures++
+ if (next == null || fallbackFailures > MAX_FALLBACK_FAILURES) {
+ app.myspoti.host.android.Logbook.log("local: Spotify did not play $title either, ${if (next == null) "no song after it" else "$fallbackFailures songs failed in a row"}: stopped")
+ fallbackFailures = 0; spotifyFallbackId = null; fallbackRest = null
+ android.widget.Toast.makeText(Graph.app, Graph.app.getString(app.myspoti.engine.R.string.song_unavailable, title), android.widget.Toast.LENGTH_LONG).show()
+ return
+ }
+ app.myspoti.host.android.Logbook.log("local: Spotify did not play $title within ${FALLBACK_START_MS / 1000} s: the queue goes on with ${next.title}")
+ android.widget.Toast.makeText(Graph.app, Graph.app.getString(app.myspoti.engine.R.string.song_unavailable_skipped, title), android.widget.Toast.LENGTH_SHORT).show()
+ val name = rest!!.third
+ playThroughExtensions(next.spotifyTrackId, next, name, fallback = {
+ // The next one is not on the extensions either: Spotify is tried for it, watched the same way.
+ spotifyPlayed.add(next.spotifyTrackId)
+ fallbackRest = Triple(next.spotifyTrackId, songs.drop(1), name)
+ playSong(buildSpotifyPlayQuery(next.spotifyTrackId, next.title, next.singer), positionMs = null, askExtensions = false)
+ watchFallback(next.spotifyTrackId, next.title)
+ }) { isLatest, _ ->
+ com.music.spotui.extensions.ExtensionAudio.playList(songs, next, name, shuffle = false, isLatest = isLatest)
+ .also { if (it) fallbackFailures = 0 }
+ }
+ }
+
/** What picking an audio source in the quality sheet did to the song playing ([switchSource]). */
enum class SourceSwitch {
/** The song plays from it, from where it was (or already did). */
@@ -1400,6 +1449,10 @@ object SongPlayer {
positionMs = end.takeIf { it > 0 }, askExtensions = false)
}
+ /** How long a song sent to Spotify for want of the extensions may take to start there. */
+ private const val FALLBACK_START_MS = 10_000L
+ private const val MAX_FALLBACK_FAILURES = 3
+
/** How long before its end the last song is played by Spotify, so its autoplay goes on at once. */
private const val END_LEAD_MS = 1_000L