Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
While Spotify limits the audio licences (429), the answer the app gives the page in Spotify's place carries the CORS headers and a Retry-After with the hold's time left, so the page reads a 429 instead of a network failure, and the hold pauses playback once instead of before each song [skip ci]
kota-zob6653118b5c9
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.ktindex 7a456b29..fcbb1b90 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@@ -160,6 +160,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop@Volatile var playbackHeld: () -> Boolean = { false }/** The song last paused by the hold: paused once, so a play the user asks for on it stands. */@Volatile private var heldPausedUri: Uri? = null+ /** The page was seen paused during the hold: its pause took, no other song is paused by it. */+ @Volatile private var heldPauseTook = false/** Audio licences Spotify refused (403) in a row, see [onLicence]. */private val licenceRefusals = LicenceRefusals(LICENCE_REFUSALS, LICENCE_REFUSALS_MS)@@ -1283,10 +1285,12 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop// Reported (2.0.2-beta.3, log seen): Spotify refusing the audio licences (429), the page// skips on by itself through every song it gets no licence for, to the end of the list and// round again. The hold's one pause was sent while the page was loading the next song and- // never took (the page kept playing for minutes): each song it settles on while held is- // paused, once.- if (!playbackHeld()) heldPausedUri = null- else if (s?.item != null && !s.isPaused && !s.isBuffering && s.item?.uri != heldPausedUri) {+ // never took (the page kept playing for minutes): a song it settles on while held is+ // paused, once. Only until the hold's pause took (reported: "a pause before each song"+ // through the hold, every song the user played on paused again).+ if (!playbackHeld()) { heldPausedUri = null; heldPauseTook = false }+ else if (s?.item != null && s.isPaused) heldPauseTook = true+ else if (!heldPauseTook && s?.item != null && !s.isPaused && !s.isBuffering && s.item?.uri != heldPausedUri) {heldPausedUri = s.item?.uribridge.logger?.invoke("playback held (Spotify limiting): ${s.item?.name} paused")scope.launch { runCatching { pause() } }diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.ktindex eae17e22..5a1f8053 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt@@ -226,11 +226,20 @@ class WebPlayerHost(private val context: Context) : BridgeHost {// skips on by itself through every song it gets no licence for (measured in the 25/10// logs: 20 to 25 more licence requests in the 4 s after the hold began, ~4 a second,// every one answered 429). Those are not sent: the page gets the same 429 from here- // and Spotify sees no more of them while the hold lasts.+ // and Spotify sees no more of them while the hold lasts. With the headers Spotify's own+ // answer has for the page (log seen, 2.0.2-beta.5: without them the page read a CORS+ // failure, status 0, not a 429) and the hold's time left.if (request.method == "POST" && licenseLimited() && url.contains(LICENSE_PATH)) {if (heldLicenses++ == 0) Logbook.log("audio licence requests answered here while Spotify is limiting")app.myspoti.spotify.report.Refusal402.licenceHeld()- return WebResourceResponse("application/octet-stream", "utf-8", 429, "Too Many Requests", emptyMap(), ByteArrayInputStream(ByteArray(0)))+ val left = ((_licenseLimitedUntil.value - android.os.SystemClock.elapsedRealtime()) / 1000).coerceAtLeast(1)+ val headers = mapOf(+ "Access-Control-Allow-Origin" to PLAYER_URL.trimEnd('/'),+ "Access-Control-Allow-Credentials" to "true",+ "Access-Control-Expose-Headers" to "Retry-After",+ "Retry-After" to "$left",+ )+ return WebResourceResponse("application/octet-stream", "utf-8", 429, "Too Many Requests", headers, ByteArrayInputStream(ByteArray(0)))}// The sign-in flow lives on accounts.spotify.com: logging its steps is what turns// "the login is stuck" into the exact request that failed (with the status from