Skip to content

Native Android client for Spotify, built with Jetpack Compose

Transport commands pull playback back to this phone when another device took it without being picked in Myspoti

kota-zo8a4689956cd5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ab8c0e8..11c011dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ Each release on GitHub shows its section below. The app shows it too when it off
- **Myspoti's icon in the notification** and in Samsung's Now Bar, instead of a generic music note.
- **Ads that get through are silent**: they are muted on the page itself, no longer through a Spotify Connect volume command that some accounts refuse (the ad was heard).
- **Cover back to its full size** in the player.
+- **Myspoti's buttons play on this phone again** when another device (the Spotify app on the same account, for instance) took playback without being picked in Myspoti: the music comes back here first, instead of driving that device (where skips, previous and volume were refused).
- **Music no longer stops for a minute**: a Web Player page that stops answering is reloaded at once, and playback goes on.
- **No more silent songs**: the sound comes back by itself if a skip or a muted ad left it at zero; after a skip it comes back as soon as the next song is there.
- **Smart shuffle** goes on from where the song is (no restart), adds songs to every playlist, and has its own icon in the player.
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 c528fad3..22c071ef 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
@@ -453,6 +453,7 @@ object SongPlayer {
scope.launch {
transportLock.withLock {
if (gen != transportGen.get()) return@withLock
+ takeBackPlayback()
runCatching { kotlinx.coroutines.withTimeout(TRANSPORT_TIMEOUT_MS) { block { gen == transportGen.get() } } }
.onFailure { Log.w(TAG, "transport failed: ${it.message}") }
}
@@ -563,6 +564,8 @@ object SongPlayer {
/** Tracks shufflePlay plays without a context: the first one, plus up to 49 queued. */
private const val SHUFFLE_QUEUE_MAX = 50
+ /** After pulling playback back, the page needs a moment before it takes commands. */
+ private const val PULL_BACK_SETTLE_MS = 600L
/** Shuffle-play a list: Spotify picks the first track of its own shuffled context. */
fun shufflePlay(songs: List<SongsModel>) {
@@ -796,9 +799,28 @@ object SongPlayer {
return list
}
+ /** The other device picked in Myspoti to play on; null: this phone. */
+ @Volatile private var chosenRemote: String? = null
+
+ /**
+ * Another device took playback without being picked here (the Spotify app opened on the
+ * same account, for instance): Myspoti's play, skip and previous then drove that device,
+ * where a free account's commands are refused. The music comes back to this phone first.
+ */
+ private suspend fun takeBackPlayback() {
+ val remote = _activeDevice.value ?: return
+ if (remote.id == chosenRemote) return
+ Log.i(TAG, "playback taken by ${remote.name}: pulled back here")
+ runCatching { api.devices.pullHere() }.onFailure { Log.w(TAG, "pull here failed: ${it.message}") }
+ _activeDevice.value = null
+ PlaybackService.setPlaybackDevice(false, remoteVolume)
+ kotlinx.coroutines.delay(PULL_BACK_SETTLE_MS)
+ }
+
/** Move playback to [device] (Spotify Connect transfer). */
fun transferTo(device: ConnectDevice) {
Haptics.confirm()
+ chosenRemote = if (device.isSelf) null else device.id
_activeDevice.value = if (device.isSelf) null else device
command { if (device.isSelf) api.devices.pullHere() else api.devices.transfer(device.id) }
}