Skip to content

Native Android client for Spotify, built with Jetpack Compose

A pause during an ad stays a pause: the watcher that gets past an ad still on after three seconds played the next song (or reloaded and resumed the song before the ad), starting the music the user had paused; now it reloads the page once with nothing resumed and leaves the pause as it is, and the user's resume plays on from the song before the ad, in one play, when the new page has no song of its own [skip ci]

kota-zo7084491ae04f
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 cb00fd00..25ca72ec 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
@@ -83,6 +83,11 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
@Volatile private var adMuteTries = 0
/** A pause asked through [pause]; it holds until playback is asked for again, never expires. */
@Volatile private var pausedByUser = false
+ /**
+ * The song before an ad the user paused during, got past by a reload with nothing resumed (see
+ * the watcher): the user's next [resume] plays on from it when the new page has no song of its own.
+ */
+ @Volatile private var onwardAfterPausedAd: PlayerState? = null
/** The order given to the last [play] (context uri to songs), kept for a stall restart. */
@Volatile private var lastOrder: Pair<Uri, List<Uri>>? = null
@@ -712,6 +717,22 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
if (stuck && licenceRefusedHold) {
bridge.logger?.invoke("ad stuck on ${ad?.item?.name}: left as it is, Spotify refuses this account's licences")
adSince = System.currentTimeMillis()
+ } else if (stuck && pausedByUser) {
+ // The user paused during the ad: getting past it never plays (a play of the next
+ // song, or a reload that resumed the song, started the music the user had paused).
+ // The page is reloaded once with nothing resumed, as for a mirror; the user's
+ // resume plays on from the song before the ad if the new page has none.
+ if (!adReloaded && reloadHost != null) {
+ adAttempts++
+ adReloaded = true
+ muteUntilSong = true
+ // A song picked before the pause is not played either.
+ pickBeforeAd = null
+ onwardAfterPausedAd = lastTrackState
+ bridge.logger?.invoke("ad stuck on ${ad?.item?.name}: paused by the user, reloading the player with nothing resumed (attempt $adAttempts)")
+ reloadAndPlayOn(null)
+ } else bridge.logger?.invoke("ad stuck on ${ad?.item?.name}: paused by the user, left as it is")
+ adSince = System.currentTimeMillis()
} else if (stuck) {
adAttempts++
val last = lastTrackState
@@ -1287,6 +1308,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
noteTrackChange(lastTrackState, s, adJustEnded)
lastTrackState = s
_state.value = s
+ // A song plays: nothing left to play on from after a paused ad.
+ if (s?.item != null && !s.isPaused) onwardAfterPausedAd = null
// 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
@@ -1635,6 +1658,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
/** [play], from the user or from a pick of theirs played again here; [origin] for the log. */
private suspend fun playAsked(contextUri: Uri, trackUri: Uri?, index: Int?, positionMs: Long?, order: List<Uri>?, origin: String) {
userMoveAt = System.currentTimeMillis()
+ onwardAfterPausedAd = null
// The ad due after the song that played before: a new play brings its own queue, and the
// put-back of an earlier ad skip would queue that old song after the pick.
adRestore?.cancel()
@@ -1785,6 +1809,17 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
override suspend fun resume() {
userPlays()
+ // An ad the user paused during was got past by a reload with nothing resumed: a new page
+ // with no song of its own has nothing to resume, so the music goes on from the song before
+ // the ad, as the recovery would have played it (one play).
+ val onward = onwardAfterPausedAd
+ onwardAfterPausedAd = null
+ if (onward != null && !_adPlaying.value && _state.value?.item == null && liveIntent() == null) {
+ pausedByUser = false
+ bridge.logger?.invoke("resume after an ad got past while paused: playing on from ${onward.item?.name}")
+ playOnNewPage { playOn(onward) }
+ return
+ }
resumePage()
}
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyPlayerCharacterizationTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyPlayerCharacterizationTest.kt
index 8e908e5a..6ed06599 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyPlayerCharacterizationTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyPlayerCharacterizationTest.kt
@@ -149,4 +149,53 @@ class SpotifyPlayerCharacterizationTest {
waitFor("break over") { !player.adPlaying.value }
waitFor("resume sent", ms = SpotifyPlayer.AD_END_SETTLE_MS + 8_000) { sent("player.resume").isNotEmpty() }
}
+
+ /** A page reloaded by the host: the old one goes, the new one says ready and reports nothing playing. */
+ @Volatile private var reloads = 0
+
+ private fun reloadingHost() {
+ player.reloadHost = {
+ reloads++
+ scope.launch {
+ delay(500)
+ bridge.pageGone()
+ delay(300)
+ host.post("""{"type":"ready"}""")
+ delay(300)
+ host.post("""{"type":"state","state":{"track":null,"contextUri":null,"next":null,"isPaused":true,"isBuffering":false,
+ |"positionMs":0,"timestamp":${System.currentTimeMillis()},"restrictions":{}}}""".trimMargin())
+ }
+ }
+ }
+
+ private val pausedAd get() =
+ """{"type":"state","state":{"track":$ad,"contextUri":"spotify:playlist:C","next":null,"isPaused":true,"isBuffering":false,
+ |"positionMs":0,"timestamp":${System.currentTimeMillis()},"restrictions":{}}}""".trimMargin()
+
+ @Test fun `an ad paused by the user that the watcher gets past starts no music, the user's resume plays once`() = runBlocking {
+ reloadingHost()
+ adBreak()
+ player.pause()
+ waitFor("pause sent") { sent("player.pause").isNotEmpty() }
+ host.post(pausedAd)
+ // Past the delay after which the watcher gets past a playing ad (it used to play the next song).
+ waitFor("the ad got past", ms = SpotifyPlayer.AD_PLAYING_SKIP_MS + 15_000) { reloads > 0 || sent("player.play").isNotEmpty() }
+ waitFor("break over") { !player.adPlaying.value }
+ delay(2_500)
+ assertTrue(sent("player.play").isEmpty() && sent("player.resume").isEmpty(), "music started after the user's pause: ${host.calls.map { it.first }}")
+ assertEquals(1, reloads)
+ // The user plays again: the music goes on from the song before the ad, in one play.
+ player.resume()
+ waitFor("play after the resume") { sent("player.play").isNotEmpty() }
+ delay(1_500)
+ assertEquals(1, sent("player.play").size, "one play: ${host.calls.map { it.first }}")
+ assertTrue(sent("player.resume").isEmpty(), "nothing to resume on a page without a song: ${host.calls.map { it.first }}")
+ }
+
+ /** The other side of the test above: nobody paused, the watcher gets past the ad and the music goes on. */
+ @Test fun `an ad nobody paused is got past and the music goes on`() = runBlocking {
+ reloadingHost()
+ adBreak()
+ waitFor("music on after the ad", ms = SpotifyPlayer.AD_PLAYING_SKIP_MS + 15_000) { sent("player.play").isNotEmpty() }
+ }
}