Skip to content

Native Android client for Spotify, built with Jetpack Compose

Relaunch with an extension: the mirror no longer stops the local player because Spotify still lists the previous page device while the page settles, and the saved position is kept current every 10 s (an app closed mid-song resumed from its start)

kota-zo2b269920fb5d
diff --git a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
index 878abdd4..d392711f 100644
--- a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
+++ b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
@@ -538,6 +538,16 @@ object ConnectMirror {
/** Another device of the account took playback while mirrored: the local player stops, as it would have. */
private fun wentAway() {
if (!running || want?.paused != false) return
+ // The page just (re)loaded: Spotify still lists this phone's previous page device as the one
+ // playing until it is taken off (a second or two after a relaunch). That is not another device:
+ // seen as one, the extension's song was stopped right after a relaunch (reported). Checked
+ // again once the page has settled.
+ val left = recoverUntil - SystemClock.elapsedRealtime()
+ if (left > 0) {
+ log("another device seems to play while the page settles: checked again in ${left / 1000} s")
+ main.postDelayed({ if (page.playsElsewhere.value) wentAway() }, left + 500)
+ return
+ }
log("another device took playback: the local player stops")
handedAway = true
// Ends the mirror too (SongPlayer.unmuteMirror); nothing to pause here, the page stands still.
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 f8508e43..a8003338 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
@@ -482,6 +482,24 @@ object SongPlayer {
.apply()
}
+ /**
+ * The saved session's position kept current while a song plays (every [POSITION_SAVE_MS]): saved on
+ * track changes and play/pause only, an app closed mid-song resumed it from its start (reported
+ * with an extension's song after a relaunch).
+ */
+ private val savePosition = object : Runnable {
+ override fun run() {
+ main.postDelayed(this, POSITION_SAVE_MS)
+ if (!(playing || LocalPlayer.isPlaying())) return
+ val p = lastTrackPrefs()
+ val uri = p.getString("uri", "").orEmpty()
+ val now = if (LocalPlayer.active.value) localSpotifyUri(LocalPlayer.current()) else _nowPlaying.value.uri
+ if (uri.isBlank() || uri != now) return
+ p.edit().putLong("position", getCurrentPosition()).apply()
+ }
+ }
+ init { main.postDelayed(savePosition, POSITION_SAVE_MS) }
+
private fun acceptsSessionUpdates() = Graph.app.getSharedPreferences("myspoti_prefs", Context.MODE_PRIVATE)
.getBoolean("LoggedIn", false)
@@ -1450,6 +1468,8 @@ object SongPlayer {
positionMs = end.takeIf { it > 0 }, askExtensions = false)
}
+ private const val POSITION_SAVE_MS = 10_000L
+
/** 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