Skip to content

Native Android client for Spotify, built with Jetpack Compose

A song asked right after a launch, while the extensions are still starting, is asked again once they are (their "none" then only meant they were not asked), and is not held on Spotify for the session while that answer is awaited

kota-zo768d6ea2f9c2
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 03c7bfed..f8508e43 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
@@ -1245,7 +1245,8 @@ object SongPlayer {
if (!played) main.post {
if (isLatest()) {
app.myspoti.host.android.Logbook.log("audio: no extension had ${song?.title ?: trackId}, Spotify plays")
- spotifyPlayed.add(trackId)
+ // Not when its answer is still awaited: the song goes back to the extension then.
+ if (!com.music.spotui.extensions.ExtensionAudio.awaiting(trackId)) spotifyPlayed.add(trackId)
routingState.value = null; spotifyFallbackId = trackId; fallback()
}
}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt b/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt
index 82c4ac58..35c06bea 100644
--- a/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt
+++ b/base/app/src/main/java/com/music/spotui/extensions/ExtensionAudio.kt
@@ -148,6 +148,15 @@ object ExtensionAudio {
private val warming = ConcurrentHashMap.newKeySet<String>()
+ /** The songs whose extension answer is still awaited after Spotify started them ([playList]). */
+ private val awaited = ConcurrentHashMap.newKeySet<String>()
+
+ /** Whether the answer for [trackId] is still awaited: Spotify plays it for now, not for want of the extensions. */
+ fun awaiting(trackId: String): Boolean = trackId in awaited
+
+ /** How long a song asked right after launch waits for the extensions to finish starting. */
+ private const val START_WAIT_MS = 20_000L
+
/** Asks that outlive a tap's wait (see [playList]). */
private val late = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.SupervisorJob() + Dispatchers.IO)
@@ -266,16 +275,23 @@ object ExtensionAudio {
// takes it back to the extension where it is ([late]), rather than the user doing so by hand.
picking.incrementAndGet()
val cached = known[picked.spotifyTrackId]?.takeIf { it.fresh }?.stream
+ // Right after a launch the extension may still be starting: its "none" then only means it
+ // was not asked. It is asked again once started (reported: a resume after a relaunch went to
+ // Spotify, and the song was then held there for the whole session).
+ val wasStarting = Extensions.starting()
+ val info = Extensions.trackInfo(picked.spotifyTrackId, picked.title, picked.singer, picked.album, picked.durationMs.toLong(), picked.coverUri)
val ask = if (cached != null) null else late.async {
- Extensions.audio(Extensions.trackInfo(picked.spotifyTrackId, picked.title, picked.singer, picked.album, picked.durationMs.toLong(), picked.coverUri), timeoutMs = Extensions.AUDIO_MS)
+ Extensions.audio(info, timeoutMs = Extensions.AUDIO_MS)
+ ?: if (wasStarting && Extensions.awaitStarted(START_WAIT_MS)) Extensions.audio(info, timeoutMs = Extensions.AUDIO_MS) else null
}
val first = try {
cached ?: withTimeoutOrNull(Extensions.PICK_AUDIO_MS + 1_000) { ask!!.await() }
} finally { picking.decrementAndGet() } ?: run {
if (ask != null && !ask.isCompleted) {
app.myspoti.host.android.Logbook.log("audio: no extension audio for ${picked.title} within ${(Extensions.PICK_AUDIO_MS + 1_000) / 1000} s: Spotify plays it, the extension's answer is still awaited")
+ awaited.add(picked.spotifyTrackId)
late.launch {
- val s = runCatching { ask.await() }.getOrNull() ?: return@launch
+ val s = try { runCatching { ask.await() }.getOrNull() } finally { awaited.remove(picked.spotifyTrackId) } ?: return@launch
keep(picked.spotifyTrackId, s)
withContext(Dispatchers.Main) { com.music.spotui.di.SongPlayer.extensionAnsweredLate(picked.spotifyTrackId) }
}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
index 92636c66..76b1a1b6 100644
--- a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
+++ b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
@@ -745,6 +745,12 @@ object Extensions {
return isReady && (providing(Hook.AUDIO_SOURCE).isNotEmpty() || startingAudioSource())
}
+ /** The extensions on at launch are still being started. */
+ fun starting(): Boolean = !started.isCompleted
+
+ /** Waits up to [ms] for the extensions on at launch to be started; true once they are. */
+ suspend fun awaitStarted(ms: Long): Boolean = started.isCompleted || withTimeoutOrNull(ms) { started.await() } != null
+
/** An audio-source extension is turned on (running or not yet): a session of theirs goes back to them. */
fun audioSourceOn(): Boolean = isReady && _registry.value.extensions.any { it.enabled && Action.AUDIO_SOURCE.key in it.granted }