Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Stop the music when a skip cannot reach the Connect device

Lelonio1cb70aaac7ed
diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
index eec6771..5f61c03 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -83,6 +83,9 @@ class LibrespotPlayer(
// that skipping quickly could produce.
handler.post {
if (released) return@post
+ // A load is the one command with no way around the Connect
+ // device, so when that is gone the track simply never changes.
+ // Build a new one first and load on the other side of it.
// Read now, not when this was scheduled. The fade takes a
// moment, and a tap on a track while paused arrives as "load
// this" followed immediately by "play": with the flag frozen at
@@ -236,6 +239,10 @@ class LibrespotPlayer(
.onFailure { android.util.Log.e("SquarePlayer", "$what did not reach the engine", it) }
}
+ /** Whether the engine says its Connect device is gone; false if it cannot answer. */
+ private val deviceGone: Boolean
+ get() = runCatching { NativeBridge.spircLost }.getOrDefault(false)
+
/** Call after any queue mutation, before [invalidateState]. */
private fun onQueueChanged() {
cachedPlaylist = null
@@ -325,6 +332,13 @@ class LibrespotPlayer(
if (positionMs > 0) engine("seek") { NativeBridge.seek(positionMs) }
}
+ // Nothing is listening for a skip, and nothing here can bring the
+ // device back: librespot hands out the Spirc builder once per
+ // session, so a second one cannot be built on this one. Stopping is
+ // what the engine's own fallback does, and saying so here keeps the
+ // two branches below from pretending otherwise.
+ deviceGone -> engine("next") { NativeBridge.next() }
+
// A step either way is a skip, and Spirc has to be the one making
// it: reloading the queue for a skip would restart the context and
// show up on other devices as a new session rather than a next.
@@ -779,6 +793,7 @@ class LibrespotPlayer(
*/
const val SKIP_SETTLE_MS = 220L
+
/** Ducked volume, as a fraction of the current one. */
const val DUCK_FACTOR = 0.3
diff --git a/native/src/engine.rs b/native/src/engine.rs
index fc051a2..a084912 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -942,7 +942,18 @@ fn transport(
});
}
- with_engine(|engine| match via_spirc(engine) {
+ with_engine(|engine| match {
+ // Taken over first, the same way a load does.
+ //
+ // A device that is registered but not the account's active one drops
+ // every transport command on the floor, and says so at warning level:
+ // "SpircCommand::Play will be ignored while Not Active". Nothing came
+ // back as an error, so the app saw a pause that had been accepted and a
+ // player that went on regardless. The load path has taken this step
+ // since it was written; the transport path never did.
+ let _ = engine.spirc.activate();
+ via_spirc(engine)
+ } {
Ok(()) => Ok(()),
Err(e) => {
log::warn!("{what}: spirc refused it ({e}), going straight to the player");
@@ -973,12 +984,18 @@ pub fn seek(position_ms: u32) -> EngineResult<()> {
with_engine(|e| e.spirc.set_position_ms(position_ms))?.map_err(|e| format!("seek failed: {e}"))
}
+// Skipping is a Spirc command like the rest, and it was the one left out of
+// [`transport`]: with the device gone it was accepted, discarded, and the track
+// that was already playing carried on. Stopping is the fallback because there
+// is nothing better available here. The player owns no queue, so it cannot pick
+// the next track itself, and going on with the current one is the one answer
+// that is certainly wrong: the listener asked for something else.
pub fn next() -> EngineResult<()> {
- with_engine(|e| e.spirc.next())?.map_err(|e| format!("next failed: {e}"))
+ transport("next", |e| e.spirc.next(), |e| e.player.stop())
}
pub fn previous() -> EngineResult<()> {
- with_engine(|e| e.spirc.prev())?.map_err(|e| format!("previous failed: {e}"))
+ transport("previous", |e| e.spirc.prev(), |e| e.player.stop())
}
pub fn set_shuffle(shuffle: bool) -> EngineResult<()> {