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
Keep the video in a corner, or keep only its sound
Leloniofd379902c326
diff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.ktindex 845606f..26d803a 100644--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt@@ -412,6 +412,18 @@ class PlaybackService : MediaLibraryService() {kindFor(container.preferences.backend.value)}if (wanted == playerKind) return++ // Leaving the video behind, if that is what was in front.+ //+ // Choosing a song somewhere else while a video plays is a choice to+ // listen to that song: the player swapped underneath, but the screen was+ // never told the video had ended, so it went on drawing the video's slot+ // — a black rectangle over a song that was playing perfectly well, with+ // the switch back to audio pointing at a video that no longer existed.+ if (playerKind == PlayerKind.VIDEO) {+ dev.lelonio.square.backend.spotify.SpotifyVideoMode.setEnabled(false)+ dev.lelonio.square.backend.spotify.SpotifyVideoMode.setManifest(null)+ }// 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)diff --git a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt b/app/src/main/java/dev/lelonio/square/ui/MainActivity.ktindex 2029b4b..913d540 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt@@ -27,6 +27,9 @@ import dev.lelonio.square.playback.PlaybackService* it, and holding one from a backgrounded app leaks a binder connection.*/@UnstableApi+private const val ACTION_LISTEN = "dev.lelonio.square.LISTEN_ONLY"+private const val ACTION_TOGGLE = "dev.lelonio.square.PIP_TOGGLE"+class MainActivity : ComponentActivity() {private var controller by mutableStateOf<MediaController?>(null)@@ -98,24 +101,111 @@ class MainActivity : ComponentActivity() {*/override fun onUserLeaveHint() {super.onUserLeaveHint()- if (!dev.lelonio.square.backend.youtube.YouTubeVideoMode.enabled.value) return+ // Either kind of video: YouTube's, and the music video Spotify has for+ // a track. Leaving the app while watching one means the same thing in+ // both cases.+ val watching = dev.lelonio.square.backend.youtube.YouTubeVideoMode.enabled.value ||+ dev.lelonio.square.backend.spotify.SpotifyVideoMode.enabled.value+ if (!watching) return+ runCatching { enterPictureInPictureMode(pipParams()) }+ }++ /**+ * The floating window, and the one button in it.+ *+ * The button is the thing YouTube puts there: keep the sound, drop the+ * picture. Pausing to close a video is the wrong end of what the listener+ * asked for — they left the app, so the music is what they are keeping.+ */+ private fun pipParams(): android.app.PictureInPictureParams {val size = controller?.videoSizeval width = size?.width?.takeIf { it > 0 } ?: 16val height = size?.height?.takeIf { it > 0 } ?: 9- runCatching {- enterPictureInPictureMode(- android.app.PictureInPictureParams.Builder()- .setAspectRatio(android.util.Rational(width, height))- .build(),- )++ // Play and pause first, because that is the one control anybody+ // expects a floating video to have.+ val playing = controller?.isPlaying == true+ val transport = android.app.RemoteAction(+ android.graphics.drawable.Icon.createWithResource(+ this,+ if (playing) {+ dev.lelonio.square.R.drawable.ic_pip_pause+ } else {+ dev.lelonio.square.R.drawable.ic_pip_play+ },+ ),+ getString(if (playing) dev.lelonio.square.R.string.pause else dev.lelonio.square.R.string.play),+ getString(if (playing) dev.lelonio.square.R.string.pause else dev.lelonio.square.R.string.play),+ android.app.PendingIntent.getBroadcast(+ this,+ 1,+ android.content.Intent(ACTION_TOGGLE).setPackage(packageName),+ android.app.PendingIntent.FLAG_UPDATE_CURRENT or+ android.app.PendingIntent.FLAG_IMMUTABLE,+ ),+ )++ val listen = android.app.RemoteAction(+ android.graphics.drawable.Icon.createWithResource(this, dev.lelonio.square.R.drawable.ic_headphones),+ getString(dev.lelonio.square.R.string.pip_listen),+ getString(dev.lelonio.square.R.string.pip_listen),+ android.app.PendingIntent.getBroadcast(+ this,+ 0,+ android.content.Intent(ACTION_LISTEN).setPackage(packageName),+ android.app.PendingIntent.FLAG_UPDATE_CURRENT or+ android.app.PendingIntent.FLAG_IMMUTABLE,+ ),+ )++ return android.app.PictureInPictureParams.Builder()+ .setAspectRatio(android.util.Rational(width, height))+ .setActions(listOf(transport, listen))+ .build()+ }++ /**+ * Told by the button above: the picture goes, the song stays.+ *+ * Registered while the activity is alive and not exported — nothing outside+ * this app has any business asking for it.+ */+ private val listenRequest = object : android.content.BroadcastReceiver() {+ override fun onReceive(context: android.content.Context?, intent: android.content.Intent?) {+ if (intent?.action == ACTION_TOGGLE) {+ controller?.let { if (it.isPlaying) it.pause() else it.play() }+ return+ }+ if (intent?.action != ACTION_LISTEN) return+ val at = controller?.currentPosition ?: 0L+ if (dev.lelonio.square.backend.spotify.SpotifyVideoMode.enabled.value) {+ dev.lelonio.square.backend.spotify.SpotifyVideoMode.listen(at)+ } else {+ controller?.let(dev.lelonio.square.backend.youtube.YouTubeVideoMode::toggle)+ }+ // Said before finishing, and this matters: the flag is the app's,+ // not the window's, and finishing from inside a floating window+ // does not always report the window closing. Left set, the next+ // time the app was opened it drew the floating window's layout —+ // a black rectangle with a video surface and nothing else.+ dev.lelonio.square.backend.youtube.YouTubeVideoMode.setPictureInPicture(false)++ // Closing the window rather than going back to a full screen the+ // listener has already left. The music is in the service, and it+ // carries on.+ finish()}}+ /** Whether this activity is the floating window right now. */+ private var inPictureInPicture = false+override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean,newConfig: android.content.res.Configuration,) {super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)+ inPictureInPicture = isInPictureInPictureModedev.lelonio.square.backend.youtube.YouTubeVideoMode.setPictureInPicture(isInPictureInPictureMode,)@@ -147,6 +237,12 @@ class MainActivity : ComponentActivity() {getBooleanExtra(dev.lelonio.square.playback.EXTRA_OPEN_PLAYER, false)override fun onStart() {+ androidx.core.content.ContextCompat.registerReceiver(+ this,+ listenRequest,+ android.content.IntentFilter(ACTION_LISTEN).apply { addAction(ACTION_TOGGLE) },+ androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED,+ )super.onStart()val token = SessionToken(this, ComponentName(this, PlaybackService::class.java))val future = MediaController.Builder(this, token).buildAsync()@@ -154,12 +250,22 @@ class MainActivity : ComponentActivity() {{// The activity may already be stopping by the time this lands.controller = runCatching { future.get() }.getOrNull()+ // The floating window's own button has to say what it does now,+ // and the system only redraws it when the parameters are set+ // again.+ controller?.addListener(object : androidx.media3.common.Player.Listener {+ override fun onIsPlayingChanged(isPlaying: Boolean) {+ if (!inPictureInPicture) return+ runCatching { setPictureInPictureParams(pipParams()) }+ }+ })},MoreExecutors.directExecutor(),)}override fun onStop() {+ runCatching { unregisterReceiver(listenRequest) }controller?.release()controller = nullsuper.onStop()diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.ktindex 3b72fd2..1f0fd67 100644--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt@@ -637,7 +637,15 @@ fun SquareApp(.background(Color.Black),contentAlignment = Alignment.Center,) {- player?.let { dev.lelonio.square.ui.player.VideoSurface(it) }+ // The key matters for a Spotify video: its player is built+ // fresh when the mode is entered, and a surface attached to the+ // one before it shows nothing at all.+ player?.let {+ dev.lelonio.square.ui.player.VideoSurface(+ it,+ attachKey = if (spotifyVideoOn) spotifyVideoGeneration else null,+ )+ }}}returndiff --git a/app/src/main/res/drawable/ic_headphones.xml b/app/src/main/res/drawable/ic_headphones.xmlnew file mode 100644index 0000000..25362d5--- /dev/null+++ b/app/src/main/res/drawable/ic_headphones.xml@@ -0,0 +1,16 @@+<?xml version="1.0" encoding="utf-8"?><!--+ For the floating window's own button: keep the sound, drop the picture.++ A drawable rather than one of the app's Compose icons because this one is+ drawn by the system, in a window this app does not compose.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="256"+ android:viewportHeight="256"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="#FFFFFFFF"+ android:pathData="M201.89,54.66A103.43,103.43,0,0,0,128.79,24H128A104,104,0,0,0,24,128v56a24,24,0,0,0,24,24H64a24,24,0,0,0,24-24V152a24,24,0,0,0-24-24H40.36A88.12,88.12,0,0,1,128,40h0.68a87.55,87.55,0,0,1,61.9,25.95A88.11,88.11,0,0,1,215.64,128H192a24,24,0,0,0-24,24v32a24,24,0,0,0,24,24h16a24,24,0,0,0,24-24V128A103.41,103.41,0,0,0,201.89,54.66ZM64,144a8,8,0,0,1,8,8v32a8,8,0,0,1-8,8H48a8,8,0,0,1-8-8V144Zm152,40a8,8,0,0,1-8,8H192a8,8,0,0,1-8-8V152a8,8,0,0,1,8-8h24Z" />+</vector>diff --git a/app/src/main/res/drawable/ic_pip_pause.xml b/app/src/main/res/drawable/ic_pip_pause.xmlnew file mode 100644index 0000000..5203247--- /dev/null+++ b/app/src/main/res/drawable/ic_pip_pause.xml@@ -0,0 +1,8 @@+<?xml version="1.0" encoding="utf-8"?><!-- For the floating window, which the system draws. -->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp" android:height="24dp"+ android:viewportWidth="256" android:viewportHeight="256"+ android:tint="#FFFFFFFF">+ <path android:fillColor="#FFFFFFFF"+ android:pathData="M200,32H168a16,16,0,0,0-16,16V208a16,16,0,0,0,16,16h32a16,16,0,0,0,16-16V48A16,16,0,0,0,200,32ZM88,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H88a16,16,0,0,0,16-16V48A16,16,0,0,0,88,32Z" />+</vector>diff --git a/app/src/main/res/drawable/ic_pip_play.xml b/app/src/main/res/drawable/ic_pip_play.xmlnew file mode 100644index 0000000..9e53d47--- /dev/null+++ b/app/src/main/res/drawable/ic_pip_play.xml@@ -0,0 +1,8 @@+<?xml version="1.0" encoding="utf-8"?><!-- For the floating window, which the system draws. -->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp" android:height="24dp"+ android:viewportWidth="256" android:viewportHeight="256"+ android:tint="#FFFFFFFF">+ <path android:fillColor="#FFFFFFFF"+ android:pathData="M232,128a15.76,15.76,0,0,1-7.33,13.34L88.09,229.9A15.91,15.91,0,0,1,64,216.56V39.44A15.91,15.91,0,0,1,88.09,26.1l136.58,88.56A15.76,15.76,0,0,1,232,128Z" />+</vector>