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.

Play a playlist as a playlist, not as a list of its tracks

Lelonioc7031b79eb91
diff --git a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
index e08d19a..60109df 100644
--- a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logc
a
_/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  ��А�3  ��߈�3`
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  ��А�3� ��߈�3e
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  �����3� ��߈�3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  ��А�3� ��߈�3R
+a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  �����3� ��߈�3R
P
-N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  ��А�3� ��߈�3V
+N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  �����3� ��߈�3V
T
-R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  ��А�3[
+R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  �����3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  ��А�3 @ ��߈�3\
+W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  �����3 @ ��߈�3\
Z
-X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  ��А�3֚ ��߈�3`
+X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  �����3֚ ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  ��А�3 �" ��߈�3f
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  �����3 �" ��߈�3f
d
-b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  ��А�3
+b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  �����3
� ��߈�3Y
W
-U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  ��А�3  ( ��߈�3^
+U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  �����3  ( ��߈�3^
\
-Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  ��А�3  Y ��߈�3D
+Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  �����3  Y ��߈�3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  ��А�3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
diff --git a/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt b/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
index f28fecc..1a90d88 100644
--- a/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
@@ -81,6 +81,8 @@ object NativeBridge {
positionMs: Int = 0,
/** The playlist or album the queue came from; empty when it came from none. */
contextUri: String = "",
+ /** Hand playback to Spotify as that context rather than as loose tracks. */
+ playAsContext: Boolean = false,
) =
nativeLoadQueue(
Json.encodeToString(ListSerializer(String.serializer()), uris),
@@ -88,6 +90,7 @@ object NativeBridge {
startPlaying,
positionMs,
contextUri,
+ playAsContext,
)
fun play() = nativePlay()
@@ -167,6 +170,7 @@ object NativeBridge {
startPlaying: Boolean,
positionMs: Int,
contextUri: String,
+ playAsContext: Boolean,
)
private external fun nativeNext()
private external fun nativePrevious()
diff --git a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
index 41452bd..6c3454a 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
@@ -75,6 +75,10 @@ class LibrespotPlayer(
startPlaying,
positionMs,
queue.contextUri.orEmpty(),
+ // Only when the queue is that context in its own order, and
+ // not while shuffled: the engine would then play Spotify's
+ // order and the list on screen would be someone else's.
+ playAsContext = queue.contextIsOrdered && !queue.isShuffled,
)
}
.onFailure { android.util.Log.e("SpotPlayer", "load failed: ${it.message}") }
diff --git a/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt b/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
index a0d819e..11d128c 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
@@ -2,6 +2,7 @@ package dev.emanuele.spot.playback
import android.net.Uri
import androidx.media3.common.MediaItem
+import dev.emanuele.spot.ui.EXTRA_CONTEXT_ORDERED
import dev.emanuele.spot.ui.EXTRA_CONTEXT_URI
/**
@@ -166,6 +167,8 @@ class PlayQueue {
contextUri = mediaItems.firstNotNullOfOrNull {
it.mediaMetadata.extras?.getString(EXTRA_CONTEXT_URI)
}
+ contextIsOrdered = mediaItems.firstOrNull()
+ ?.mediaMetadata?.extras?.getBoolean(EXTRA_CONTEXT_ORDERED) == true
replace(mediaItems.mapNotNull(::toTrack), startIndex)
}
@@ -178,6 +181,10 @@ class PlayQueue {
var contextUri: String? = null
private set
+ /** True when this queue is that context in its own order; see LibrespotPlayer. */
+ var contextIsOrdered: Boolean = false
+ private set
+
/** Insert controller-supplied items; see [replaceFromMediaItems] for the id rule. */
fun addFromMediaItems(index: Int, mediaItems: List<MediaItem>) {
add(index, mediaItems.mapNotNull(::toTrack))
diff --git a/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt b/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
index 918c09e..a834c47 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
@@ -100,9 +100,14 @@ class MainActivity : ComponentActivity() {
}
/** Sends the visible list to the session, starting at the tapped track. */
- private fun play(tracks: List<CatalogTrack>, index: Int, contextUri: String? = null) {
+ private fun play(
+ tracks: List<CatalogTrack>,
+ index: Int,
+ contextUri: String? = null,
+ asContext: Boolean = false,
+ ) {
val player = controller ?: return
- player.setMediaItems(tracks.map { toMediaItem(it, contextUri) }, index, 0L)
+ player.setMediaItems(tracks.map { toMediaItem(it, contextUri, asContext) }, index, 0L)
player.prepare()
player.play()
}
@@ -123,7 +128,11 @@ class MainActivity : ComponentActivity() {
}
}
- private fun toMediaItem(track: CatalogTrack, contextUri: String? = null): MediaItem =
+ private fun toMediaItem(
+ track: CatalogTrack,
+ contextUri: String? = null,
+ asContext: Boolean = false,
+ ): MediaItem =
MediaItem.Builder()
// The media id carries the Spotify URI; PlayQueue refuses anything else.
.setMediaId(track.uri)
@@ -139,7 +148,10 @@ class MainActivity : ComponentActivity() {
// channel between them that survives the session boundary.
.setExtras(
contextUri?.let {
- android.os.Bundle().apply { putString(EXTRA_CONTEXT_URI, it) }
+ android.os.Bundle().apply {
+ putString(EXTRA_CONTEXT_URI, it)
+ putBoolean(EXTRA_CONTEXT_ORDERED, asContext)
+ }
},
)
.build(),
@@ -149,3 +161,6 @@ class MainActivity : ComponentActivity() {
/** Key for the context URI carried in a media item's metadata extras. */
const val EXTRA_CONTEXT_URI = "dev.emanuele.spot.CONTEXT_URI"
+
+/** Whether that queue is the context in its own order; see SpotApp's `onPlay`. */
+const val EXTRA_CONTEXT_ORDERED = "dev.emanuele.spot.CONTEXT_ORDERED"
diff --git a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
index 3afbf04..6829b7c 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
@@ -154,11 +154,15 @@ private val BackdropBlur = BlurTransformation(radius = 14, passes = 2)
fun SpotApp(
player: Player?,
/**
- * Plays a queue. The third argument is the context it came from — a
- * playlist or album URI — which is what the account's listening history is
- * filed under; null when the tracks are a selection rather than a place.
+ * Plays a queue.
+ *
+ * The third argument is the context it came from — a playlist or album URI
+ * — which is what the account's listening history is filed under; null when
+ * the tracks are a selection rather than a place. The fourth says whether
+ * the queue *is* that context in its own order, in which case playback is
+ * handed to Spotify as the context itself.
*/
- onPlay: (List<CatalogTrack>, Int, String?) -> Unit,
+ onPlay: (List<CatalogTrack>, Int, String?, Boolean) -> Unit,
/** Appends one track to the end of the queue; see the swipe gesture on rows. */
onEnqueue: (CatalogTrack) -> Unit,
/**
@@ -388,7 +392,9 @@ fun SpotApp(
onOpenPlaylist = { navController.openPlaylist(viewModel, it) },
playlistOrder = playlistOrder,
recent = recent,
- onPlayRecent = { tracks, index -> onPlay(tracks, index, null) },
+ onPlayRecent = { tracks, index ->
+ onPlay(tracks, index, null, false)
+ },
feed = feed,
onOpenItem = { item ->
viewModel.openContext(item.uri, item.title, item.artworkUrl)
@@ -408,7 +414,9 @@ fun SpotApp(
onQueryChange = viewModel::onSearchQuery,
onClientIdChange = viewModel::onWebApiClientIdChange,
onConnectWebApi = { viewModel.connectWebApi() },
- onPlayTrack = { tracks, index -> onPlay(tracks, index, null) },
+ onPlayTrack = { tracks, index ->
+ onPlay(tracks, index, null, false)
+ },
onEnqueue = onEnqueue,
onOpenContext = { item ->
viewModel.openContext(item.uri, item.title, item.artworkUrl)
@@ -460,8 +468,8 @@ fun SpotApp(
contentPadding = listPadding,
nowPlayingUri = playback.mediaId,
onBack = { navController.popBackStack() },
- onPlay = { tracks, index ->
- onPlay(tracks, index, playlist.uri)
+ onPlay = { tracks, index, asContext ->
+ onPlay(tracks, index, playlist.uri, asContext)
},
onEnqueue = onEnqueue,
onShuffle = { tracks ->
@@ -469,7 +477,10 @@ fun SpotApp(
// treats shuffle as a mode and stamps it
// onto whatever queue arrives next.
player?.shuffleModeEnabled = true
- onPlay(tracks, 0, playlist.uri)
+ // Shuffled: the order on screen is not
+ // the one that will play, so this is a
+ // selection rather than the context.
+ onPlay(tracks, 0, playlist.uri, false)
},
onAddToPlaylist = { track ->
viewModel.openAddToPlaylist(track.uri, track.name)
@@ -625,7 +636,7 @@ fun SpotApp(
if (menu != null) {
TrackSheetAction("Riproduci", PhosphorIcons.Fill.Play) {
trackMenu = null
- onPlay(listOf(menu.track), 0, null)
+ onPlay(listOf(menu.track), 0, null, false)
}
TrackSheetAction("Aggiungi alla coda", PhosphorIcons.Regular.Queue) {
trackMenu = null
diff --git a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
index 84d9023..dd015f9 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
@@ -112,7 +112,12 @@ fun PlaylistScreen(
contentPadding: PaddingValues,
nowPlayingUri: String?,
onBack: () -> Unit,
- onPlay: (List<CatalogTrack>, Int) -> Unit,
+ /**
+ * Plays the list. The flag says whether what is being played is the context
+ * in its own order — false once the rows are sorted or filtered, when the
+ * queue is a selection out of the playlist rather than the playlist.
+ */
+ onPlay: (List<CatalogTrack>, Int, Boolean) -> Unit,
onEnqueue: (CatalogTrack) -> Unit,
onShuffle: (List<CatalogTrack>) -> Unit,
/** Opens the app-wide "add to playlist" sheet for one track. */
@@ -142,6 +147,12 @@ fun PlaylistScreen(
}
var sortOpen by remember { mutableStateOf(false) }
+ // Whether the rows on screen are the playlist itself, in its order. Sorting
+ // or searching makes them a selection out of it, and playing that as the
+ // context would have Spotify play the playlist's own order instead of the
+ // one on screen.
+ val asContext = sort == TrackSort.ORIGINAL && query.isBlank()
+
// Derived, not stored: keeping a second list in state would leave the two
// able to disagree after a reload.
val visible = remember(state.tracks, query, sort) {
@@ -265,7 +276,9 @@ fun PlaylistScreen(
collapsedHeight = collapsedHeight,
topPadding = topPadding,
onBack = onBack,
- onPlay = { visible.takeIf { it.isNotEmpty() }?.let { onPlay(it, 0) } },
+ onPlay = {
+ visible.takeIf { it.isNotEmpty() }?.let { onPlay(it, 0, asContext) }
+ },
onShuffle = { visible.takeIf { it.isNotEmpty() }?.let(onShuffle) },
onToggleSearch = {
searching = !searching
@@ -368,7 +381,7 @@ fun PlaylistScreen(
track = track,
position = index + 1,
isCurrent = track.uri == nowPlayingUri,
- onClick = { onPlay(visible, index) },
+ onClick = { onPlay(visible, index, asContext) },
onMenu = { onTrackMenu(track) },
)
}
diff --git a/native/src/engine.rs b/native/src/engine.rs
index dddeba5..b00c3b8 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -676,6 +676,7 @@ pub fn load_queue(
start_playing: bool,
position_ms: u32,
context_uri: String,
+ play_as_context: bool,
) -> EngineResult<()> {
if uris.is_empty() {
return Err("empty queue".into());
@@ -687,8 +688,9 @@ pub fn load_queue(
}
}
- if let Ok(mut context) = CONTEXT_URI.lock() {
- *context = context_uri;
+ let context = context_uri.clone();
+ if let Ok(mut stored) = CONTEXT_URI.lock() {
+ *stored = context_uri;
}
let options = LoadRequestOptions {
@@ -698,12 +700,35 @@ pub fn load_queue(
..LoadRequestOptions::default()
};
+ // Handed over as the context itself when the queue really is one.
+ //
+ // A list of URIs is not a place: played that way, the account sees a queue
+ // of loose tracks, other devices see no playlist, and the listen has
+ // nowhere to be filed. The caller says when the two agree — the rows on
+ // screen being the playlist in its own order — because only it can know.
+ let request = if play_as_context {
+ let start = uris
+ .get(index as usize)
+ .cloned()
+ .map(PlayingTrack::Uri)
+ .or(Some(PlayingTrack::Index(index)));
+ LoadRequest::from_context_uri(
+ context,
+ LoadRequestOptions {
+ playing_track: start,
+ ..options
+ },
+ )
+ } else {
+ LoadRequest::from_tracks(uris, options)
+ };
+
with_engine(|e| {
// Without this the device is registered but idle, and a load is
// ignored: playback belongs to whichever device the account has
// active, and taking that over is an explicit step.
e.spirc.activate()?;
- e.spirc.load(LoadRequest::from_tracks(uris, options))
+ e.spirc.load(request)
})?
.map_err(|e| format!("load failed: {e}"))
}
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index a238c2f..43eb1a5 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -116,6 +116,7 @@ pub extern "system" fn Java_dev_emanuele_spot_nativecore_NativeBridge_nativeLoad
start_playing: jboolean,
position_ms: jint,
context_uri: JString,
+ play_as_context: jboolean,
) {
// JSON rather than a jobjectArray: the Kotlin side already serialises track
// lists for the catalogue calls, and one decoder is easier to keep correct
@@ -132,6 +133,7 @@ pub extern "system" fn Java_dev_emanuele_spot_nativecore_NativeBridge_nativeLoad
start_playing == JNI_TRUE,
position_ms.max(0) as u32,
context,
+ play_as_context == JNI_TRUE,
)
});
or_throw(&mut env, result);