Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Play the music that is already on the phone
Lelonio517d4da2adf3
diff --git a/app/src/main/java/dev/lelonio/square/data/LocalLibrary.kt b/app/src/main/java/dev/lelonio/square/data/LocalLibrary.ktindex 3f0a320..9979b5f 100644--- a/app/src/main/java/dev/lelonio/square/data/LocalLibrary.kt+++ b/app/src/main/java/dev/lelonio/square/data/LocalLibrary.kt@@ -32,6 +32,26 @@ object LocalLibrary {/** The URI scheme local tracks carry, so every screen can tell them apart. */const val PREFIX = "local:track:"+ /**+ * The list itself, which is a context like any playlist.+ *+ * It is not one on any service, so it has no id to be addressed by: this is+ * a name the app gives its own shelf, and everything that opens a track+ * list recognises it the same way it recognises a playlist URI.+ */+ const val CONTEXT_URI = "local:files"++ fun isLocalContext(uri: String?): Boolean = uri == CONTEXT_URI++ /**+ * What the shelf carries where a playlist carries a cover URL.+ *+ * Not a URL at all: there is no picture to fetch for a folder of files, and+ * the tile is drawn by the app at whatever size it is asked for. See+ * Artwork, which recognises this and hands over to its own drawing.+ */+ const val COVER = "square:local-files-cover"+/*** The permission needed to read any of this.*@@ -80,6 +100,7 @@ object LocalLibrary {MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.DURATION,+ MediaStore.Audio.Media.DATE_ADDED,)runCatching {@@ -98,6 +119,7 @@ object LocalLibrary {val album = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)val albumId = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)val duration = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)+ val added = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_ADDED)buildList {while (cursor.moveToNext()) {@@ -113,6 +135,11 @@ object LocalLibrary {album = cursor.getString(album).orEmpty(),durationMs = cursor.getLong(duration),artworkUrl = artworkUri(cursor.getLong(albumId)),+ // The same field a playlist carries, so the+ // "recently added" order works here too. The+ // index counts in seconds; everything else in+ // this app reads an ISO-8601 stamp.+ addedAt = isoStamp(cursor.getLong(added)),),)}@@ -131,6 +158,14 @@ object LocalLibrary {* former is one URI the loader can cache, and the latter would mean opening* and decoding every file to build a list.*/+ /** Seconds since the epoch, as the rest of the app spells a date. */+ private fun isoStamp(seconds: Long): String? =+ if (seconds <= 0) {+ null+ } else {+ java.time.Instant.ofEpochSecond(seconds).toString()+ }+private fun artworkUri(albumId: Long): String? =if (albumId <= 0) null else "content://media/external/audio/albumart/$albumId"diff --git a/app/src/main/java/dev/lelonio/square/data/PlaylistOrderStore.kt b/app/src/main/java/dev/lelonio/square/data/PlaylistOrderStore.ktindex 6c5fbf0..cecb719 100644--- a/app/src/main/java/dev/lelonio/square/data/PlaylistOrderStore.kt+++ b/app/src/main/java/dev/lelonio/square/data/PlaylistOrderStore.kt@@ -60,6 +60,16 @@ class PlaylistOrderStore(context: Context) {* opened must not be shuffled among themselves, or the tail of the list would* rearrange itself every time one of them was touched.*/+/**+ * The phone's own music first, whatever the rest are sorted by.+ *+ * A fixed shelf rather than a playlist among playlists; see LocalLibrary.+ */+fun List<CatalogPlaylist>.withLocalFilesFirst(): List<CatalogPlaylist> {+ val local = firstOrNull { LocalLibrary.isLocalContext(it.uri) } ?: return this+ return listOf(local) + filterNot { it.uri == local.uri }+}+fun List<CatalogPlaylist>.sortedByRecentlyOpened(order: List<String>): List<CatalogPlaylist> {if (order.isEmpty()) return thisval rank = order.withIndex().associate { (index, uri) -> uri to index }diff --git a/app/src/main/java/dev/lelonio/square/playback/LocalPlayerFactory.kt b/app/src/main/java/dev/lelonio/square/playback/LocalPlayerFactory.ktnew file mode 100644index 0000000..89dd055--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/LocalPlayerFactory.kt@@ -0,0 +1,82 @@+package dev.lelonio.square.playback++import androidx.annotation.OptIn+import androidx.media3.common.C+import androidx.media3.common.Player+import androidx.media3.common.util.UnstableApi+import androidx.media3.datasource.DefaultDataSource+import androidx.media3.datasource.ResolvingDataSource+import androidx.media3.exoplayer.ExoPlayer+import androidx.media3.exoplayer.source.DefaultMediaSourceFactory+import dev.lelonio.square.backend.PlaybackHost+import dev.lelonio.square.data.LocalLibrary++/**+ * The player for music that is already on the phone.+ *+ * Nothing to fetch and nothing to decrypt, so this is ExoPlayer as it comes,+ * reading a file through the content resolver. The only piece of plumbing is+ * the resolver below.+ */+@OptIn(UnstableApi::class)+object LocalPlayerFactory {++ fun create(host: PlaybackHost): Player = ExoPlayer.Builder(host.context, renderers(host))+ .setMediaSourceFactory(+ DefaultMediaSourceFactory(+ ResolvingDataSource.Factory(+ DefaultDataSource.Factory(host.context),+ // The queue carries `local:track:<id>`, which is an+ // identity rather than a location: it survives being+ // written to disk and read back next week, where a+ // `content://` URI is only meaningful while the row it+ // points at still exists. The location is worked out here,+ // at the moment of opening the file.+ ResolvingDataSource.Resolver { spec ->+ val uri = spec.uri.toString()+ if (LocalLibrary.isLocal(uri)) {+ spec.buildUpon().setUri(LocalLibrary.contentUri(uri)).build()+ } else {+ spec+ }+ },+ ),+ ),+ )+ .setHandleAudioBecomingNoisy(true)+ // Ducks and pauses for whatever else wants the speaker, as the YouTube+ // player does; the Spotify path gets this from its own output.+ .setAudioAttributes(+ androidx.media3.common.AudioAttributes.Builder()+ .setUsage(C.USAGE_MEDIA)+ .setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)+ .build(),+ /* handleAudioFocus = */ true,+ )+ .setLooper(host.looper)+ .build()++ /**+ * The sink, with the app's own vocoder in its chain.+ *+ * Speed and pitch otherwise fall to the platform's stretcher, which is+ * built for speech and sounds like it on music — and the same song altered+ * the same amount would sound worse from the phone than from Spotify. See+ * BungeeAudioProcessor.+ */+ private fun renderers(host: PlaybackHost) =+ object : androidx.media3.exoplayer.DefaultRenderersFactory(host.context) {+ override fun buildAudioSink(+ context: android.content.Context,+ enableFloatOutput: Boolean,+ enableAudioTrackPlaybackParams: Boolean,+ ) = androidx.media3.exoplayer.audio.DefaultAudioSink.Builder(context)+ .setEnableFloatOutput(enableFloatOutput)+ // The hardware path applies speed itself, below the processors,+ // which would leave the vocoder with nothing to do and the+ // platform doing it after all.+ .setEnableAudioTrackPlaybackParams(false)+ .setAudioProcessorChain(BungeeProcessorChain())+ .build()+ }+}diff --git a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.ktindex 766d9a0..afde699 100644--- a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt+++ b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt@@ -54,6 +54,15 @@ import kotlinx.coroutines.sync.withLockclass MediaBrowseTree(context: Context,private val scope: CoroutineScope,+ /**+ * Puts the right player behind the session for a queue about to be set.+ *+ * The service's own, passed in rather than reached for: this class is the+ * one place every queue goes through, whoever set it — the app, the car,+ * an assistant — and the music on the phone plays through a different+ * player than either streaming service. See PlaybackService.ensurePlayerFor.+ */+ private val ensurePlayerFor: suspend (String?) -> Unit = {},) : MediaLibrarySession.Callback {private val app = context.applicationContext as SquareApplication@@ -388,6 +397,11 @@ class MediaBrowseTree(startIndex: Int,startPositionMs: Long,): ListenableFuture<MediaSession.MediaItemsWithStartPosition> = scope.future {+ ensurePlayerFor(+ mediaItems.getOrNull(startIndex.coerceAtLeast(0))?.mediaId+ ?: mediaItems.firstOrNull()?.mediaId,+ )+val single = mediaItems.singleOrNull()val parts = single?.mediaId?.let(::splitTrackId)?: return@future MediaSession.MediaItemsWithStartPosition(diff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.ktindex 50568a4..3d7aa22 100644--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt@@ -85,7 +85,7 @@ class PlaybackService : MediaLibraryService() {crossfade = container.crossfadeplayer = buildPlayer(container.preferences.backend.value)- val browseTree = MediaBrowseTree(this, scope)+ val browseTree = MediaBrowseTree(this, scope, ::ensurePlayerFor)session = MediaLibrarySession.Builder(this, player, browseTree)// Without this the notification is inert to a tap: Media3 has no way// to know which activity owns the session. `SINGLE_TOP` so an app@@ -173,6 +173,12 @@ class PlaybackService : MediaLibraryService() {// network was a second of silence for an answer that is often wrong,// and the engine can now be told a new bitrate without one.+ // The room follows the slider on whichever player is in front. The+ // Spotify path takes it below, in its own output; this is for the rest.+ scope.launch {+ AudioEffects.reverb.collect { followReverb() }+ }+// Which stretcher does speed and pitch. Unlike the bitrate this one is// switchable while playing: it is a property of the output, not of the// session.@@ -229,7 +235,101 @@ class PlaybackService : MediaLibraryService() {* The YouTube half needs none of it — ExoPlayer owns its own output — so* this is the one place the two genuinely differ.*/+ /**+ * Which player is behind the session.+ *+ * A backend each, and one more for the music already on the phone: those+ * files belong to no service, they are listed in both libraries, and+ * neither backend's player can open them. So what decides this is not only+ * the setting but what is being played; see [ensurePlayerFor].+ */+ private enum class PlayerKind { SPOTIFY, YOUTUBE, LOCAL }++ private var playerKind = PlayerKind.SPOTIFY++ /**+ * The Spotify player while a local file is in front of it.+ *+ * Kept rather than released because releasing it stops the engine, and the+ * engine is what every catalogue read goes through: a phone playing an mp3+ * would otherwise have no library.+ */+ private var parkedSpotify: LibrespotPlayer? = null++ /**+ * The room, for whichever player is not the Spotify engine.+ *+ * That path has its own: the reverb hangs off the AudioTrack the engine+ * writes into. ExoPlayer takes the same effect through its own attachment,+ * so a file on the phone gets the same room rather than none; see+ * [PlayerReverb].+ */+ private val exoReverb = PlayerReverb()++ /** Puts the room on the player in front, or takes it off the one leaving. */+ private fun followReverb() {+ val exo = player as? androidx.media3.exoplayer.ExoPlayer ?: return+ exoReverb.apply(exo, AudioEffects.reverb.value)+ }++ /**+ * Note on starting with the local player, which this deliberately does not+ * do: the Spotify engine is started by the player that needs it, and+ * everything the catalogue does needs it too. Coming up with the local+ * player behind the session left the engine unstarted, so the library sat+ * on its spinner for ever while a local file played perfectly. A queue of+ * local files therefore does not survive a restart; it is put back the+ * moment one is played again.+ */+ private fun kindFor(backendId: dev.lelonio.square.backend.BackendId) = when (backendId) {+ dev.lelonio.square.backend.BackendId.SPOTIFY -> PlayerKind.SPOTIFY+ dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC -> PlayerKind.YOUTUBE+ }++ /**+ * Puts the right player behind the session for what is about to play.+ *+ * Called before a queue is handed over, because a local file arriving at+ * the Spotify engine is a track it cannot fetch and a Spotify URI arriving+ * at the local player is a file that is not there. Returns having swapped+ * only when it had to: the ordinary case is a queue of the same kind as the+ * last one, which is nothing at all.+ */+ suspend fun ensurePlayerFor(firstUri: String?) {+ val wanted = if (firstUri != null && dev.lelonio.square.data.LocalLibrary.isLocal(firstUri)) {+ PlayerKind.LOCAL+ } else {+ kindFor(container.preferences.backend.value)+ }+ if (wanted == playerKind) return+ // Nothing to put back: the queue that caused this swap is about to be+ // set on the new player by the session itself.+ swapPlayer(wanted, restore = false)+ }++ private fun buildPlayer(+ backendId: dev.lelonio.square.backend.BackendId,+ ): androidx.media3.common.Player = buildPlayer(kindFor(backendId))+private fun buildPlayer(+ kind: PlayerKind,+ ): androidx.media3.common.Player = when (kind) {+ PlayerKind.LOCAL -> {+ librespot = null+ playerKind = kind+ LocalPlayerFactory.create(playbackHost).also { built ->+ built.playbackParameters = androidx.media3.common.PlaybackParameters(+ AudioEffects.speed.value,+ AudioEffects.pitch.value,+ )+ }+ }++ PlayerKind.SPOTIFY -> buildBackendPlayer(dev.lelonio.square.backend.BackendId.SPOTIFY)+ PlayerKind.YOUTUBE -> buildBackendPlayer(dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC)+ }++ private fun buildBackendPlayer(backendId: dev.lelonio.square.backend.BackendId,): androidx.media3.common.Player = when (backendId) {dev.lelonio.square.backend.BackendId.SPOTIFY -> {@@ -267,7 +367,7 @@ class PlaybackService : MediaLibraryService() {)}}- }+ }.also { playerKind = kindFor(backendId) }/*** Swaps the player under the running session.@@ -277,10 +377,26 @@ class PlaybackService : MediaLibraryService() {* so the player behind it can be replaced.*/private fun switchBackend(backendId: dev.lelonio.square.backend.BackendId) = scope.launch {- runCatching { savePlayback() }+ swapPlayer(kindFor(backendId))+ }++ /** The swap itself; see [switchBackend] and [ensurePlayerFor]. */+ private suspend fun swapPlayer(kind: PlayerKind, restore: Boolean = true) {+ // Only a player with something in it has anything to save, and saving+ // an empty one does not write nothing: it clears the store. At startup+ // the player being swapped away from is the one just built, empty, so+ // the first act of restoring a queue was deleting it.+ if (player.mediaItemCount > 0) runCatching { savePlayback() }saveJob?.cancel()- if (librespot != null) {+ // The engine goes only when another *service* takes over.+ //+ // Playing a file from the phone is not leaving Spotify: the session is+ // what the whole catalogue is read through, so shutting it down to play+ // an mp3 left the library unable to answer anything until the listener+ // went back to a Spotify track. It stays up, and the local player takes+ // the speaker for as long as it is in front.+ if (librespot != null && kind == PlayerKind.YOUTUBE) {// Off the main thread, and this is why the switch is a coroutine at// all: shutting the native engine down means stopping its threads// and closing its session, which took long enough to hang the input@@ -289,6 +405,67 @@ class PlaybackService : MediaLibraryService() {runCatching { withContext(Dispatchers.IO) { NativeBridge.shutdown() } }engineStarted = false}+ // Going to a local file: the Spotify player is set aside rather than+ // released.+ //+ // Releasing it shuts the engine down — that is what its own release+ // does — and the engine is what the whole catalogue is read through.+ // Worse, it went down while the flag saying it was up stayed set, so+ // coming back built a player over an engine nobody restarted and every+ // command answered "engine not started". Parked, it keeps the session+ // alive behind the local player and is picked up again on the way back,+ // already configured.+ (player as? androidx.media3.exoplayer.ExoPlayer)?.let(exoReverb::release)++ val parking = librespot != null && kind == PlayerKind.LOCAL+ if (parking) {+ runCatching { player.playWhenReady = false }+ parkedSpotify = librespot+ librespot = null+ playerKind = kind+ player = LocalPlayerFactory.create(playbackHost).also { built ->+ built.playbackParameters = androidx.media3.common.PlaybackParameters(+ AudioEffects.speed.value,+ AudioEffects.pitch.value,+ )+ }+ session?.player = player+ // The queue on disk belongs to this player when it is the one being+ // restored into — leaving it out is what made a local song paused+ // at closing time impossible to start again: the screen had it, the+ // player had nothing.+ if (restore) restoreTimeline(container.preferences.backend.value)+ followReverb()+ observeForSaving()+ return+ }++ // Coming back to a parked one: nothing to build and nothing to connect.+ val parked = parkedSpotify+ if (parked != null && kind == PlayerKind.SPOTIFY) {+ runCatching {+ player.stop()+ player.clearMediaItems()+ }+ runCatching { player.release() }+ parkedSpotify = null+ librespot = parked+ playerKind = kind+ player = parked+ session?.player = player+ if (restore) restoreQueue()+ observeForSaving()+ return+ }++ // Anything else is a real change of source, and the parked player has+ // no place in it.+ parkedSpotify?.let { stale ->+ parkedSpotify = null+ runCatching { stale.release() }+ engineStarted = false+ }+// Emptied before it goes: the session keeps reporting whatever the old// player had loaded until something replaces it, and the new source's// queue may legitimately be empty — which would leave the previous@@ -302,9 +479,23 @@ class PlaybackService : MediaLibraryService() {// Video belongs to the source that was playing it.dev.lelonio.square.backend.youtube.YouTubeVideoMode.reset()- player = buildPlayer(backendId)+ player = buildPlayer(kind)session?.player = player- startSpotifyEngineIfActive()+ followReverb()+ // The engine is what the Spotify player plays through, so it is started+ // whatever brought us here — without this, coming back from a local file+ // left a player wired to an engine that was never connected, and nothing+ // from Spotify would play again until the app was restarted.+ //+ // What is conditional is putting the last queue back: when the swap was+ // caused by a queue arriving, that queue is moments away and the saved+ // one would land on top of it.+ if (librespot != null) {+ connectEngine(restoreQueue = restore)+ } else {+ if (restore) restoreTimeline(container.preferences.backend.value)+ observeForSaving()+ }}/**@@ -321,6 +512,24 @@ class PlaybackService : MediaLibraryService() {restoreTimeline(container.preferences.backend.value)observeForSaving()}+ restoreLocalQueueIfLast()+ }++ /**+ * Puts a queue of the phone's own files back after a restart.+ *+ * It cannot be done by starting with the local player, which is what was+ * tried first: the engine is started by the Spotify player, everything the+ * catalogue does needs the engine, and coming up without it left the+ * library on its spinner for ever. So the session starts as it always did+ * and the local player is swapped in afterwards, with the engine left+ * running behind it.+ */+ private fun restoreLocalQueueIfLast() {+ val saved = runCatching { playbackStore.load() }.getOrNull() ?: return+ val first = saved.tracks.firstOrNull()?.uri ?: return+ if (!dev.lelonio.square.data.LocalLibrary.isLocal(first)) return+ scope.launch { swapPlayer(PlayerKind.LOCAL, restore = true) }}/**@@ -561,7 +770,7 @@ class PlaybackService : MediaLibraryService() {* Authenticates the native session. Runs off the main thread because the* access-point handshake is blocking.*/- private fun connectEngine() = scope.launch {+ private fun connectEngine(restoreQueue: Boolean = true) = scope.launch {// Guards against the repeated ACTION_CONNECT the UI may send: the native// side rejects a second start, and that error would look like a login// failure and clear a perfectly good session.@@ -641,7 +850,7 @@ class PlaybackService : MediaLibraryService() {}}.onSuccess {android.util.Log.i(TAG, "engine connected")- restoreQueue()+ if (restoreQueue) restoreQueue()observeForSaving()}}@@ -806,11 +1015,18 @@ class PlaybackService : MediaLibraryService() {private fun restoreTimeline(backendId: dev.lelonio.square.backend.BackendId) {if (player.mediaItemCount > 0) returnval saved = playbackStore.load() ?: return- val backend = when (backendId) {- dev.lelonio.square.backend.BackendId.SPOTIFY -> container.spotifyBackend- dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC -> container.youtubeBackend+ // The phone's own files belong to no backend, so they are matched+ // against the player that is actually behind the session rather than+ // against a catalogue.+ if (playerKind == PlayerKind.LOCAL) {+ if (saved.tracks.none { dev.lelonio.square.data.LocalLibrary.isLocal(it.uri) }) return+ } else {+ val backend = when (backendId) {+ dev.lelonio.square.backend.BackendId.SPOTIFY -> container.spotifyBackend+ dev.lelonio.square.backend.BackendId.YOUTUBE_MUSIC -> container.youtubeBackend+ }+ if (saved.tracks.none { backend.owns(it.uri) }) return}- if (saved.tracks.none { backend.owns(it.uri) }) returnplayer.setMediaItems(saved.tracks.map { track ->diff --git a/app/src/main/java/dev/lelonio/square/ui/components/Artwork.kt b/app/src/main/java/dev/lelonio/square/ui/components/Artwork.ktindex f7dbf21..d48e259 100644--- a/app/src/main/java/dev/lelonio/square/ui/components/Artwork.kt+++ b/app/src/main/java/dev/lelonio/square/ui/components/Artwork.kt@@ -13,7 +13,12 @@ import androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.graphics.Brush+import androidx.compose.foundation.background+import androidx.compose.material3.Iconimport androidx.compose.ui.graphics.Color+import com.adamglin.PhosphorIcons+import com.adamglin.phosphoricons.Regular+import com.adamglin.phosphoricons.regular.FolderSimpleimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.platform.LocalDensity@@ -51,7 +56,9 @@ fun Artwork(val density = LocalDensity.currentBox(modifier.clip(shape), contentAlignment = Alignment.Center) {- if (url != null) {+ if (url == dev.lelonio.square.data.LocalLibrary.COVER) {+ LocalFilesCover()+ } else if (url != null) {val request = remember(url, decodeSize) {ImageRequest.Builder(context).data(url)@@ -92,6 +99,35 @@ fun Artwork(* soft off-centre highlight, and the initial set large and dimmed so it reads as* texture rather than as a label.*/+/**+ * The tile for the phone's own music.+ *+ * Drawn rather than shipped as a picture: this is the one shelf in the library+ * with no artwork behind it, and it is asked for at every size from a row's+ * thumbnail to the top of a page, where a bitmap would either be soft or be+ * three bitmaps. A folder because that is what it is.+ */+@Composable+private fun LocalFilesCover() {+ Box(+ Modifier+ .fillMaxSize()+ .background(LocalFilesTile),+ contentAlignment = Alignment.Center,+ ) {+ Icon(+ PhosphorIcons.Regular.FolderSimple,+ contentDescription = null,+ tint = LocalFilesGlyph,+ modifier = Modifier.fillMaxSize(0.44f),+ )+ }+}++/** The two colours of that tile, deep enough to sit in a grid of covers. */+private val LocalFilesTile = Color(0xFF20306E)+private val LocalFilesGlyph = Color(0xFF2ECC57)+@Composableprivate fun GeneratedCover(title: String, corner: Dp) {val seed = remember(title) { title.hashCode().absoluteValue }