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.

Queue a song and let it arrive in its own time

Lelonio0a66eec41514
diff --git a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
index 9d41189..9501572 100644
--- a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
@@ -3,6 +3,7 @@ package dev.lelonio.square.backend
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import dev.lelonio.square.SquareApplication
+import dev.lelonio.square.backend.lyrics.Lossless
import dev.lelonio.square.data.Catalog
import dev.lelonio.square.data.CatalogPlaylist
import dev.lelonio.square.data.CatalogTrack
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 e48f542..e8e2681 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -825,8 +825,17 @@ class LibrespotPlayer(
mediaItems: List<MediaItem>,
): ListenableFuture<*> {
queue.addFromMediaItems(index, mediaItems)
- engineQueueStale = true
reapplyShuffle()
+ // The engine is told now, not at the end of the song.
+ //
+ // Left for later, the track a listener queued was not in the engine's
+ // list when the current one ended: the engine moved to its own next
+ // track and this side had to skip to the queued one after the fact,
+ // which is a cut — the change of track the listener hears when they
+ // press skip, in the middle of a crossfade they did not. Handed over as
+ // an order, the queued track simply *is* what comes next, and the end
+ // of the song dissolves into it like any other.
+ pushOrder()
onQueueChanged()
invalidateState()
return Futures.immediateVoidFuture()
@@ -834,8 +843,11 @@ class LibrespotPlayer(
override fun handleRemoveMediaItems(fromIndex: Int, toIndex: Int): ListenableFuture<*> {
queue.remove(fromIndex, toIndex)
- engineQueueStale = true
reapplyShuffle()
+ // A track taken out of the queue is gone from the engine's list too, and
+ // for the same reason as above: otherwise it plays anyway when the
+ // current song ends, and this side skips past it afterwards.
+ pushOrder()
onQueueChanged()
invalidateState()
return Futures.immediateVoidFuture()
@@ -1097,22 +1109,30 @@ class LibrespotPlayer(
// Handed over as an order rather than as a load. A load would restart
// the decoder, and a reordering of what comes later has no business
// interrupting the song that is playing.
- if (queue.items.isNotEmpty()) {
- engineIndex = queue.currentIndex
- runCatching {
- NativeBridge.setQueueOrder(queue.items.map { it.uri }, queue.currentIndex)
- }
- .onFailure {
- // Left for the next skip to sort out, which rebuilds the
- // queue wholesale: better a load then than a wrong order now.
- android.util.Log.w("SquarePlayer", "could not reorder: ${it.message}")
- engineQueueStale = true
- }
- }
+ pushOrder()
invalidateState()
return Futures.immediateVoidFuture()
}
+ /**
+ * Hands the engine the running order, without touching what is playing.
+ *
+ * A load would restart the decoder; a change to what comes *after* the
+ * current track has no business interrupting it. When it cannot be sent the
+ * queue is marked stale, and the next skip rebuilds it wholesale — better a
+ * load then than a wrong order now.
+ */
+ private fun pushOrder() {
+ if (queue.items.isEmpty()) return
+ engineIndex = queue.currentIndex
+ runCatching {
+ NativeBridge.setQueueOrder(queue.items.map { it.uri }, queue.currentIndex)
+ }.onFailure {
+ android.util.Log.w("SquarePlayer", "could not reorder: ${it.message}")
+ engineQueueStale = true
+ }
+ }
+
override fun handleSetRepeatMode(repeatMode: @Player.RepeatMode Int): ListenableFuture<*> {
this.repeatMode = repeatMode
engine("repeat") {
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index e1585de..f61a865 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -587,7 +587,17 @@ fun SquareApp(
val creditsLoading by viewModel.creditsLoading.collectAsStateWithLifecycle()
var lyrics by remember { mutableStateOf<Lyrics?>(null) }
- var lyricsLoading by remember { mutableStateOf(false) }
+
+ /**
+ * The track [lyrics] is the answer for.
+ *
+ * A flag set at the top of the fetch is not enough to say whether an answer
+ * is in yet: between the track changing and the effect that fetches for it
+ * starting, the flag still reads as it did for the track before — long
+ * enough for the panel to say a song has no words before anybody has asked.
+ * Naming the track the answer belongs to leaves no such moment.
+ */
+ var lyricsFor by remember { mutableStateOf<String?>(null) }
// One small question per track: is this one saved? The player draws a heart
// from the answer, and there is no other way to know without reading the
@@ -599,8 +609,11 @@ fun SquareApp(
LaunchedEffect(playback.mediaId) {
val uri = playback.mediaId
lyrics = null
- if (uri == null) return@LaunchedEffect
- lyricsLoading = true
+ lyricsFor = null
+ if (uri == null) {
+ lyricsFor = null
+ return@LaunchedEffect
+ }
// Behind the song as well; see awaitAudible. The panel shows its own
// spinner meanwhile, so the wait is visible rather than blank.
awaitAudible(localState)
@@ -614,7 +627,7 @@ fun SquareApp(
durationMs = playback.durationMs,
)
}.getOrNull()
- lyricsLoading = false
+ lyricsFor = uri
}
// The language the app is read in. Changing it re-creates the activity,
@@ -1582,11 +1595,16 @@ fun SquareApp(
},
queue = queue,
lyrics = lyrics,
- lyricsLoading = lyricsLoading,
+ // Loading until the answer in hand is this
+ // track's, which covers the wait before the
+ // fetch has even begun.
+ lyricsLoading = playback.mediaId != null &&
+ lyricsFor != playback.mediaId,
credits = credits,
creditsLoading = creditsLoading,
onWantCredits = viewModel::loadCredits,
onPlayQueueItem = { player?.seekTo(it, 0L) },
+ onRemoveQueueItem = { player?.removeMediaItem(it) },
reverb = reverb,
// Speed and pitch are set as a pair because
// PlaybackParameters carries both; changing one
@@ -2042,6 +2060,7 @@ fun SquareApp(
}
}
}
+ }
}
/**
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
index 36a8976..7e08f39 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
@@ -62,6 +62,7 @@ import com.adamglin.phosphoricons.regular.VinylRecord
import com.adamglin.phosphoricons.fill.Info
import com.adamglin.phosphoricons.regular.Info
import com.adamglin.phosphoricons.regular.X
+import dev.lelonio.square.ui.glass.pressable
/** Which panel is open below the transport controls. */
enum class PlayerPanel {
@@ -302,6 +303,21 @@ internal fun QueueList(
overflow = TextOverflow.Ellipsis,
)
}
+
+ // Not on the track being played: taking that one out is a
+ // different act — it is a skip — and it already has a button.
+ if (!entry.isCurrent) {
+ Icon(
+ PhosphorIcons.Regular.X,
+ contentDescription = stringResource(R.string.remove_from_queue),
+ tint = GlassInkDim,
+ modifier = Modifier
+ .clip(RoundedCornerShape(50))
+ .pressable({ onRemove(entry.index) }, pressedScale = 0.86f)
+ .padding(10.dp)
+ .size(16.dp),
+ )
+ }
}
}
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index ddb0878..3a193dc 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -82,7 +82,9 @@ import kotlinx.coroutines.withContext
import dev.lelonio.square.backend.spotify.panelAt
import androidx.core.graphics.scale
import dev.lelonio.square.ui.glass.pressable
+import dev.lelonio.square.ui.glass.LocalGlassEffectConfig
import dev.lelonio.square.ui.glass.backdrop.Backdrop
+import dev.lelonio.square.ui.glass.liquidGlass
import dev.lelonio.square.ui.glass.backdrop.backdrops.layerBackdrop
import dev.lelonio.square.ui.glass.backdrop.backdrops.rememberCombinedBackdrop
import dev.lelonio.square.ui.glass.backdrop.backdrops.rememberLayerBackdrop
@@ -99,6 +101,7 @@ import com.adamglin.phosphoricons.Fill
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.fill.Heart
import com.adamglin.phosphoricons.fill.Pause
+import com.adamglin.phosphoricons.fill.MicrophoneStage
import com.adamglin.phosphoricons.fill.Play
import com.adamglin.phosphoricons.fill.SkipBack
import com.adamglin.phosphoricons.fill.SkipForward
@@ -174,6 +177,8 @@ fun PlayerScreen(
/** Asked for when the credits panel is opened, and not before. */
onWantCredits: (String?) -> Unit,
onPlayQueueItem: (Int) -> Unit,
+ /** Drops one track out of the queue, by index. */
+ onRemoveQueueItem: (Int) -> Unit,
/** The music video for this track, when the catalogue has one. */
videoFileId: String? = null,
/** Whether the listener has asked to watch rather than listen. */
@@ -737,7 +742,7 @@ fun PlayerScreen(
}
Stage.QUEUE -> Box(Modifier.fillMaxSize()) {
- QueueList(queue, onPlayQueueItem)
+ QueueList(queue, onPlayQueueItem, onRemoveQueueItem)
}
Stage.DEVICES -> Box(
@@ -785,6 +790,7 @@ fun PlayerScreen(
}
}
}
+ }
Spacer(Modifier.height(20.dp))
@@ -844,7 +850,6 @@ fun PlayerScreen(
)
}
}
- Spacer(Modifier.height(14.dp))
}
// Title and artist on their own capsule, with the two