Skip to content

Native Android client for Spotify, built with Jetpack Compose

A pause during an ad holds the song picked around it: nothing plays it before the user's resume, whether the ad ends by itself (it was played at once, through the pause) or the watcher gets past it (it was dropped), and the resume plays that pick, in its order and in one play once the page's device has joined, instead of the song before the ad [skip ci]

kota-zof35d49b76cb9
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 25ca72ec..6d92f007 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
@@ -107,6 +107,11 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
* then): asked again when the ad is over (see [onState]).
*/
@Volatile private var pickBeforeAd: Pick? = null
+ /**
+ * The user's last pick around an ad they paused during ([pickBeforeAd] then): nothing plays it
+ * before their [resume], which plays it instead of the song before the ad.
+ */
+ @Volatile private var pickAfterPause: Pick? = null
/**
* A pick before an ad whose break ended in a reload of ours, or one made during a reload (see
* [playAsked]): played on the new page, however long the reload took (it is the user's),
@@ -726,7 +731,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
adAttempts++
adReloaded = true
muteUntilSong = true
- // A song picked before the pause is not played either.
+ // A song picked before the pause is not played either: it waits for the user's resume.
+ pickBeforeAd?.let { pickAfterPause = it }
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)")
@@ -1256,6 +1262,14 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
if (adJustEnded && pick != null && !AdRecoveryRules.keepsPickBeforeAd(mirroring)) {
pickBeforeAd = null
bridge.logger?.invoke("song picked before the ad: left to the mirror (the page only mirrors)")
+ } else if (adJustEnded && pick != null && pausedByUser) {
+ // Paused by the user during the ad: nothing plays before their resume, which plays the pick.
+ pickBeforeAd = null
+ val landed = if (pick.track != null) s?.item?.uri == pick.track else s?.contextUri == pick.context
+ if (!landed) {
+ pickAfterPause = pick
+ bridge.logger?.invoke("song picked before the ad: paused by the user, played on their resume")
+ }
} else if (adJustEnded && pick != null) {
pickBeforeAd = null
// Reported (beta.9): a song picked in another playlist, an ad, then the old queue went
@@ -1308,8 +1322,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
+ // A song plays, the user's pause over: nothing left to play on from after a paused ad.
+ if (s?.item != null && !s.isPaused && !pausedByUser) { onwardAfterPausedAd = null; pickAfterPause = 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
@@ -1659,6 +1673,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
private suspend fun playAsked(contextUri: Uri, trackUri: Uri?, index: Int?, positionMs: Long?, order: List<Uri>?, origin: String) {
userMoveAt = System.currentTimeMillis()
onwardAfterPausedAd = null
+ pickAfterPause = 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()
@@ -1814,6 +1829,21 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
// the ad, as the recovery would have played it (one play).
val onward = onwardAfterPausedAd
onwardAfterPausedAd = null
+ // The user's pick around an ad they paused during: played now, in one play (held by
+ // [playAsked] if an ad or a reload is still on), not the song before the ad.
+ val pick = pickAfterPause?.takeIf { pausedByUser }
+ pickAfterPause = null
+ if (pick != null) {
+ val now = _state.value
+ val landed = if (pick.track != null) now?.item?.uri == pick.track else now?.contextUri == pick.context
+ if (!landed) {
+ pausedByUser = false
+ bridge.logger?.invoke("resume after a paused ad: playing the song picked around it, ${pick.track ?: pick.context}")
+ if (_adPlaying.value) { pickBeforeAd = pick; resumePage(); return }
+ playOnNewPage { playAsked(pick.context, trackUri = pick.track, index = pick.index, positionMs = pick.positionMs, order = pick.order, origin = "pick held over a paused ad") }
+ return
+ }
+ }
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}")
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 6ed06599..8df4f8d4 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
@@ -198,4 +198,42 @@ class SpotifyPlayerCharacterizationTest {
adBreak()
waitFor("music on after the ad", ms = SpotifyPlayer.AD_PLAYING_SKIP_MS + 15_000) { sent("player.play").isNotEmpty() }
}
+
+ @Test fun `a pick then a pause during an ad that ends by itself plays nothing, the resume plays the pick once`() = runBlocking {
+ adBreak()
+ player.play("spotify:playlist:C", trackUri = "spotify:track:P", order = listOf("spotify:track:P", "spotify:track:Q"))
+ player.pause()
+ waitFor("pause sent") { sent("player.pause").isNotEmpty() }
+ // The break ends before the watcher gets past it, on the old queue's next song, paused.
+ host.post(state(track("B"), track("C2"), paused = true))
+ waitFor("break over") { !player.adPlaying.value }
+ delay(SpotifyPlayer.AD_END_SETTLE_MS + 2_500)
+ assertTrue(sent("player.play").isEmpty() && sent("player.resume").isEmpty(), "music started after the user's pause: ${host.calls.map { it.first }}")
+ player.resume()
+ waitFor("play after the resume") { sent("player.play").isNotEmpty() }
+ delay(1_500)
+ val plays = sent("player.play").map { it.second }
+ assertEquals(1, plays.size, "one play: ${host.calls.map { it.first }}")
+ assertTrue("spotify:track:P" in plays.single() && "spotify:track:Q" in plays.single(), "the pick, in its order: ${plays.single()}")
+ assertTrue(sent("player.resume").isEmpty(), "the pick is played, not the old song resumed: ${host.calls.map { it.first }}")
+ }
+
+ @Test fun `a pick then a pause during an ad the watcher gets past plays nothing, the resume plays the pick once`() = runBlocking {
+ reloadingHost()
+ adBreak()
+ player.play("spotify:playlist:C", trackUri = "spotify:track:P", order = listOf("spotify:track:P", "spotify:track:Q"))
+ player.pause()
+ waitFor("pause sent") { sent("player.pause").isNotEmpty() }
+ host.post(pausedAd)
+ 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 }}")
+ player.resume()
+ waitFor("play after the resume") { sent("player.play").isNotEmpty() }
+ delay(1_500)
+ val plays = sent("player.play").map { it.second }
+ assertEquals(1, plays.size, "one play: ${host.calls.map { it.first }}")
+ assertTrue("spotify:track:P" in plays.single() && "spotify:track:Q" in plays.single(), "the pick, in its order, not the song after A: ${plays.single()}")
+ }
}