Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Canvas preloaded from the mini player and read from cache in the first frame: the player opens on it; otherwise a cross-fade from the cover
kota-zoe07fb562cbf4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dbf01e1a..a802daaf 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,7 +14,7 @@ Each release on GitHub shows its section below. The app shows it too when it off- **Fast scrollbar** on long playlists and the library.- **Share as image** (Instagram story and more); swipe up on the cover for the lyrics; player fits foldables and wide screens.- **Settings**: autoplay on or off, accent colour, screen rotation (portrait, automatic, landscape).-- **Canvas**: looked up when the song starts and kept between openings of the player, it shows without a flash after the cover; its previous / next buttons no longer appear.+- **Canvas**: loaded while the mini player shows, so the player opens straight on it; when it is still loading, it fades in smoothly over the cover. Its previous / next buttons no longer appear.## 2.0.0-beta.6diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.ktindex 1723afaf..c528fad3 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@@ -324,10 +324,6 @@ object SongPlayer {positionAt = SystemClock.elapsedRealtime()}val trackChanged = uri != _nowPlaying.value.uri- // The Canvas is looked up when the song starts, not when the player opens.- if (trackChanged && id.isNotBlank() && !uri.startsWith("spotify:episode:") &&- app.myspoti.power.EnergySettings.state.value.policy.allowCanvas && !com.music.spotui.data.DataSaver.active- ) scope.launch(kotlinx.coroutines.Dispatchers.IO) { runCatching { Graph.api.getCanvasUrl(id) } }// Autoplay off: stop once, when the player moves on to a track it picked itself.if (item != null && item.provider == "autoplay" && uri != autoplayHandledUri && !com.music.spotui.data.preferences.AutoplayPref.on) {autoplayHandledUri = uridiff --git a/base/app/src/main/java/com/music/spotui/ui/components/AppComponents.kt b/base/app/src/main/java/com/music/spotui/ui/components/AppComponents.ktindex 764b9dd5..6958a259 100644--- a/base/app/src/main/java/com/music/spotui/ui/components/AppComponents.kt+++ b/base/app/src/main/java/com/music/spotui/ui/components/AppComponents.kt@@ -29,6 +29,7 @@ import androidx.compose.material3.SliderColorsimport androidx.compose.material3.SliderDefaultsimport androidx.compose.material3.Textimport androidx.compose.runtime.Composable+import androidx.lifecycle.compose.collectAsStateWithLifecycleimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableFloatStateOf@@ -147,6 +148,18 @@ fun MiniPlayer(navController: NavHostController) {}}+ // The Canvas of the song playing is looked up and loaded now, so the full player+ // opens on it directly instead of showing the cover first.+ val energy by app.myspoti.power.EnergySettings.state.collectAsStateWithLifecycle()+ LaunchedEffect(np.uri, energy.policy.allowCanvas, com.music.spotui.data.DataSaver.active) {+ val id = np.uri.removePrefix("spotify:track:").takeIf { np.uri.startsWith("spotify:track:") } ?: return@LaunchedEffect+ if (!energy.policy.allowCanvas || com.music.spotui.data.DataSaver.active) return@LaunchedEffect+ val url = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {+ runCatching { com.music.spotui.di.Graph.api.getCanvasUrl(id) }.getOrNull()+ } ?: return@LaunchedEffect+ CanvasPlayer.preload(context, url)+ }+// The thin bar moves ~2 px/s: 4 ticks a second look identical to 20.val frame = rememberFrameClock(songPlayingState, fps = 4)diff --git a/base/app/src/main/java/com/music/spotui/ui/components/CanvasPlayer.kt b/base/app/src/main/java/com/music/spotui/ui/components/CanvasPlayer.ktnew file mode 100644index 00000000..9c761df7--- /dev/null+++ b/base/app/src/main/java/com/music/spotui/ui/components/CanvasPlayer.kt@@ -0,0 +1,60 @@+package com.music.spotui.ui.components++import com.music.spotui.di.SongPlayer++/**+ * One Canvas player for the whole app: closing and reopening the player screen keeps the+ * clip loaded (and its last frame), instead of building a new player that starts from nothing.+ */[email protected](androidx.media3.common.util.UnstableApi::class)+object CanvasPlayer {+ private var exo: androidx.media3.exoplayer.ExoPlayer? = null+ private var url: String? = null+ private var framed: String? = null+ private val firstFrame = mutableListOf<() -> Unit>()++ /**+ * [url] is loaded and ready to draw: its first frame shows as soon as a view is attached,+ * so the player screen opens on the clip instead of the cover.+ */+ fun isReady(url: String) = this.url == url && (framed == url || exo?.playbackState == androidx.media3.common.Player.STATE_READY)++ /**+ * Loads the clip of the song now playing before the player screen opens (called while+ * the mini player is on screen, so only when the app is in use). Main thread.+ */+ fun preload(context: android.content.Context, url: String) { get(context, url) }++ fun get(context: android.content.Context, url: String): androidx.media3.exoplayer.ExoPlayer {+ val player = exo ?: run {+ val cached = androidx.media3.datasource.cache.CacheDataSource.Factory()+ .setCache(SongPlayer.mediaCache(context.applicationContext))+ .setUpstreamDataSourceFactory(androidx.media3.datasource.DefaultHttpDataSource.Factory())+ androidx.media3.exoplayer.ExoPlayer.Builder(context.applicationContext)+ .setMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory(cached))+ .build().apply {+ repeatMode = androidx.media3.common.Player.REPEAT_MODE_ALL+ volume = 0f+ playWhenReady = false+ addListener(object : androidx.media3.common.Player.Listener {+ override fun onRenderedFirstFrame() {+ framed = [email protected]+ firstFrame.toList().forEach { it() }+ }+ })+ }.also { exo = it }+ }+ if (url != this.url) {+ this.url = url+ framed = null+ player.setMediaItem(androidx.media3.common.MediaItem.fromUri(url))+ player.prepare()+ }+ return player+ }++ fun onFirstFrame(listener: () -> Unit): () -> Unit {+ firstFrame += listener+ return { firstFrame -= listener }+ }+}diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.ktindex 72ca719b..82e32c30 100644--- a/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt+++ b/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt@@ -239,13 +239,29 @@ fun PlayerScreen(navController: NavController) {)}val canvas = playerViewModel.canvasState.value- val canvasUrl = canvas.url.takeIf {- energy.policy.allowCanvas && !com.music.spotui.data.DataSaver.active && displayedTrackId.isNotBlank() && canvas.trackId == displayedTrackId+ // Read from the cache in this very frame when already looked up (the view model's state+ // only follows one frame later): the screen then opens on the Canvas, not on the cover.+ val knownCanvas = remember(displayedTrackId) { com.music.spotui.di.Graph.api.knownCanvas(displayedTrackId)?.getOrNull() }+ val canvasUrl = (if (canvas.trackId == displayedTrackId) canvas.url else knownCanvas).takeIf {+ energy.policy.allowCanvas && !com.music.spotui.data.DataSaver.active && displayedTrackId.isNotBlank()}// The cover stays until the Canvas has drawn its first frame: hiding it earlier showed// the bare background for a moment (a flash) between the cover and the clip.- var canvasShown by remember(canvasUrl) { mutableStateOf(canvasUrl != null && CanvasPlayer.hasFrame(canvasUrl)) }- val coverAlpha by androidx.compose.animation.core.animateFloatAsState(if (canvasUrl != null && canvasShown) 0f else 1f, label = "cover")+ // Prepared beforehand by the mini player: the clip is shown from the first frame, no cover.+ var canvasShown by remember(canvasUrl) { mutableStateOf(canvasUrl != null && com.music.spotui.ui.components.CanvasPlayer.isReady(canvasUrl)) }+ // Cover to Canvas as one cross-fade: the clip (full screen) fades in while the cover fades+ // out and grows a little into it, instead of the background switching at once.+ val canvasOn = canvasUrl != null && canvasShown+ // A clip already loaded shows at once; one still loading fades in over the cover.+ val readyAtOnce = remember(canvasUrl) { canvasUrl != null && com.music.spotui.ui.components.CanvasPlayer.isReady(canvasUrl) }+ val canvasProgress = remember { androidx.compose.animation.core.Animatable(if (readyAtOnce) 1f else 0f) }+ LaunchedEffect(canvasOn, readyAtOnce) {+ val target = if (canvasOn) 1f else 0f+ if (canvasOn && readyAtOnce) canvasProgress.snapTo(target)+ else canvasProgress.animateTo(target, androidx.compose.animation.core.tween(CANVAS_FADE_MS, easing = androidx.compose.animation.core.FastOutSlowInEasing))+ }+ val canvasAlpha = canvasProgress.value+ val coverAlpha = 1f - canvasProgress.value// Music video (YouTube, synced to the Spotify playback): looked up for every track// shown here, offered once its time map is known, shown in place of the artwork.val mv = com.music.spotui.video.MusicVideoController@@ -296,11 +312,12 @@ fun PlayerScreen(navController: NavController) {url = canvasUrl,onFirstFrame = { canvasShown = true },playing = songPlayingState && !showLyrics && !audioBuffering,- modifier = Modifier.fillMaxSize(),+ modifier = Modifier.fillMaxSize().alpha(canvasAlpha),)Box(modifier = Modifier.fillMaxSize()+ .alpha(canvasAlpha).background(Brush.verticalGradient(colors = listOf(@@ -392,7 +409,11 @@ fun PlayerScreen(navController: NavController) {.padding(horizontal = 24.dp).aspectRatio(1f).clip(RoundedCornerShape(8.dp))- .alpha(coverAlpha),+ .graphicsLayer {+ alpha = coverAlpha+ val grow = 1f + (1f - coverAlpha) * 0.06f+ scaleX = grow; scaleY = grow+ },model = cover,contentScale = ContentScale.Crop,contentDescription = "")@@ -1218,63 +1239,6 @@ fun PlayerMenuRow(}}}-/**- * Plays a Spotify Canvas clip: a short, muted, looping video filling the- * now-playing background. Uses a dedicated ExoPlayer (separate from the audio- * engine) released when the composable leaves. Falls back to nothing if the URL fails.- *- * Loops are served from the media cache: without it every loop re-downloaded the- * clip, and since ExoPlayer ignores the activity lifecycle it kept doing so in the- * background (#87, gigabytes per hour). It also pauses while the app is not visible.- */-/**- * One Canvas player for the whole app: closing and reopening the player screen keeps the- * clip loaded (and its last frame), instead of building a new player that starts from nothing.- */[email protected](androidx.media3.common.util.UnstableApi::class)-private object CanvasPlayer {- private var exo: androidx.media3.exoplayer.ExoPlayer? = null- private var url: String? = null- private var framed: String? = null- private val firstFrame = mutableListOf<() -> Unit>()-- /** [url]'s first frame is already drawn (the clip was on screen before). */- fun hasFrame(url: String) = framed == url && exo != null-- fun get(context: android.content.Context, url: String): androidx.media3.exoplayer.ExoPlayer {- val player = exo ?: run {- val cached = androidx.media3.datasource.cache.CacheDataSource.Factory()- .setCache(SongPlayer.mediaCache(context.applicationContext))- .setUpstreamDataSourceFactory(androidx.media3.datasource.DefaultHttpDataSource.Factory())- androidx.media3.exoplayer.ExoPlayer.Builder(context.applicationContext)- .setMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory(cached))- .build().apply {- repeatMode = androidx.media3.common.Player.REPEAT_MODE_ALL- volume = 0f- playWhenReady = false- addListener(object : androidx.media3.common.Player.Listener {- override fun onRenderedFirstFrame() {- framed = [email protected]- firstFrame.toList().forEach { it() }- }- })- }.also { exo = it }- }- if (url != this.url) {- this.url = url- framed = null- player.setMediaItem(androidx.media3.common.MediaItem.fromUri(url))- player.prepare()- }- return player- }-- fun onFirstFrame(listener: () -> Unit): () -> Unit {- firstFrame += listener- return { firstFrame -= listener }- }-}-/*** Plays a Spotify Canvas clip: a short, muted, looping video filling the* now-playing background, on the shared [CanvasPlayer] (separate from the audio engine),@@ -1288,7 +1252,7 @@ private object CanvasPlayer {@Composableprivate fun CanvasVideo(url: String, playing: Boolean, onFirstFrame: () -> Unit, modifier: Modifier = Modifier) {val context = LocalContext.current- val exo = remember(url) { CanvasPlayer.get(context, url) }+ val exo = remember(url) { com.music.spotui.ui.components.CanvasPlayer.get(context, url) }val lifecycle = androidx.compose.ui.platform.LocalLifecycleOwner.current.lifecycleval requested by androidx.compose.runtime.rememberUpdatedState(playing)val shown by androidx.compose.runtime.rememberUpdatedState(onFirstFrame)@@ -1300,7 +1264,7 @@ private fun CanvasVideo(url: String, playing: Boolean, onFirstFrame: () -> Unit,exo.playWhenReady = requested && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED)}lifecycle.addObserver(observer)- val removeFrame = CanvasPlayer.onFirstFrame { shown() }+ val removeFrame = com.music.spotui.ui.components.CanvasPlayer.onFirstFrame { shown() }onDispose {lifecycle.removeObserver(observer)removeFrame()@@ -1322,5 +1286,8 @@ private fun CanvasVideo(url: String, playing: Boolean, onFirstFrame: () -> Unit,)}+/** Cover to Canvas cross-fade. */+private const val CANVAS_FADE_MS = 550+/** Height the player needs under the artwork: top bar, title, seek bar, controls, Connect row. */private const val PLAYER_CONTROLS_DP = 450