Skip to content

Native Android client for Spotify, built with Jetpack Compose

Canvas: looked up when the song starts, read from cache at once, one player kept between openings, cover hidden only after the first frame

kota-zo6b751f58f6a0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3de829f1..dbf01e1a 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**: the video's previous / next buttons no longer flash when you come back to the player.
+- **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.
## 2.0.0-beta.6
diff --git a/base/app/src/main/java/com/music/spotui/data/api/Api.kt b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
index fd110aad..e0afb7b6 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/Api.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
@@ -730,6 +730,9 @@ class Api constructor(
return url
}
+ /** The Canvas already looked up for [trackId]: null when never asked, else its url (or none). */
+ fun knownCanvas(trackId: String): Result<String?>? = CanvasCache.map[trackId]?.let { Result.success(it.value) }
+
private object CanvasCache {
class Entry(val value: String?)
val map = java.util.concurrent.ConcurrentHashMap<String, Entry>()
diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
index c528fad3..1723afaf 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,6 +324,10 @@ 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 = uri
diff --git a/base/app/src/main/java/com/music/spotui/ui/repository/AppRepository.kt b/base/app/src/main/java/com/music/spotui/ui/repository/AppRepository.kt
index dd7c28d4..f09aa780 100644
--- a/base/app/src/main/java/com/music/spotui/ui/repository/AppRepository.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/repository/AppRepository.kt
@@ -45,4 +45,5 @@ class AppRepository constructor(private val api: Api = Graph.api) {
suspend fun provideAccount() = api.getAccount()
suspend fun provideCanvasUrl(trackId: String) = api.getCanvasUrl(trackId)
+ fun knownCanvas(trackId: String) = api.knownCanvas(trackId)
}
\ No newline at end of file
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.kt
index 7e822799..116d0331 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
@@ -242,6 +242,10 @@ fun PlayerScreen(navController: NavController) {
val canvasUrl = canvas.url.takeIf {
energy.policy.allowCanvas && !com.music.spotui.data.DataSaver.active && displayedTrackId.isNotBlank() && canvas.trackId == displayedTrackId
}
+ // 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")
// 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
@@ -286,10 +290,11 @@ fun PlayerScreen(navController: NavController) {
)
)
) {
- val pillAboveTitle = canvasUrl != null && !videoOn && mediaReady
- if (canvasUrl != null && !videoOn && mediaReady) {
+ val pillAboveTitle = canvasUrl != null && !videoOn
+ if (canvasUrl != null && !videoOn) {
CanvasVideo(
url = canvasUrl,
+ onFirstFrame = { canvasShown = true },
playing = songPlayingState && !showLyrics && !audioBuffering,
modifier = Modifier.fillMaxSize(),
)
@@ -387,7 +392,7 @@ fun PlayerScreen(navController: NavController) {
.padding(horizontal = 24.dp)
.aspectRatio(1f)
.clip(RoundedCornerShape(8.dp))
- .alpha(if (canvasUrl != null && mediaReady) 0f else 1f),
+ .alpha(coverAlpha),
model = cover,
contentScale = ContentScale.Crop,
contentDescription = "")
@@ -1222,26 +1227,71 @@ fun PlayerMenuRow(
* 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() {
+ 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),
+ * paused when the composable leaves. [onFirstFrame]: the clip is visible.
+ *
+ * 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.
+ */
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
@Composable
-private fun CanvasVideo(url: String, playing: Boolean, modifier: Modifier = Modifier) {
+private fun CanvasVideo(url: String, playing: Boolean, onFirstFrame: () -> Unit, modifier: Modifier = Modifier) {
val context = LocalContext.current
- val exo = remember(url) {
- 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)
- .setMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory(cached))
- .build().apply {
- setMediaItem(androidx.media3.common.MediaItem.fromUri(url))
- repeatMode = androidx.media3.common.Player.REPEAT_MODE_ALL
- volume = 0f
- playWhenReady = false
- prepare()
- }
- }
+ val exo = remember(url) { CanvasPlayer.get(context, url) }
val lifecycle = androidx.compose.ui.platform.LocalLifecycleOwner.current.lifecycle
val requested by androidx.compose.runtime.rememberUpdatedState(playing)
+ val shown by androidx.compose.runtime.rememberUpdatedState(onFirstFrame)
LaunchedEffect(exo, playing, lifecycle) {
exo.playWhenReady = playing && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED)
}
@@ -1250,9 +1300,11 @@ private fun CanvasVideo(url: String, playing: Boolean, modifier: Modifier = Modi
exo.playWhenReady = requested && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED)
}
lifecycle.addObserver(observer)
+ val removeFrame = CanvasPlayer.onFirstFrame { shown() }
onDispose {
lifecycle.removeObserver(observer)
- exo.release()
+ removeFrame()
+ exo.playWhenReady = false
}
}
androidx.compose.ui.viewinterop.AndroidView(
diff --git a/base/app/src/main/java/com/music/spotui/ui/viewmodel/PlayerViewModel.kt b/base/app/src/main/java/com/music/spotui/ui/viewmodel/PlayerViewModel.kt
index 0f867a06..7db7fe9f 100644
--- a/base/app/src/main/java/com/music/spotui/ui/viewmodel/PlayerViewModel.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/viewmodel/PlayerViewModel.kt
@@ -88,6 +88,14 @@ class PlayerViewModel constructor(private val currentSongState: CurrentSongState
private var canvasJob: Job? = null
fun loadCanvas(trackId: String, allowFetch: Boolean = true) {
+ // Already looked up (prefetched when the song started, or a reopened player): at once,
+ // so the screen knows before its first frame whether the Canvas or the cover shows.
+ repository.knownCanvas(trackId)?.let { known ->
+ canvasJob?.cancel(); canvasJob = null
+ canvasRequests.resolve(trackId, known.getOrNull())
+ _canvasState.value = canvasRequests.state
+ return
+ }
val request = canvasRequests.select(trackId, allowFetch) ?: return
canvasJob?.cancel()
canvasJob = null
@@ -228,6 +236,14 @@ internal class CanvasRequests {
return true
}
+ /** A result known without asking (cache). */
+ fun resolve(trackId: String, url: String?) {
+ generation++
+ state = CanvasTrackState(trackId, url?.takeIf { it.isNotBlank() })
+ loading = false
+ resolved = true
+ }
+
fun fail(request: CanvasRequest): Boolean {
if (!owns(request) || !request.fetch || !loading) return false
state = CanvasTrackState(request.trackId)