From ac2b618ff9b2d83931172388d7ea8c8fcc50d6f0 Mon Sep 17 00:00:00 2001 From: subscriptions-dot <252130413+subscriptions-dot@users.noreply.github.com> Date: Mon, 2 Nov 2026 12:29:10 +0530 Subject: [PATCH 1/3] feat(auto): show synced lyrics on Android Auto Android Auto only renders what the media session publishes, so lyrics reach the car two ways: - Now playing: while the phone is in car mode, the current synced line replaces the title row and the song moves to the smaller row. Long lines are split across both rows so they don't get cut off. The in-app player and the phone notification outside the car are unchanged. - Lyrics tab: a new root tab lists a window of lines around the current one (marked with a note and a play icon, next line as subtitle). Tapping a line seeks to it without touching the queue. Refreshes are coalesced to at most one every 500 ms. If the car can't show five tabs, Artists gives way. Both can be turned off in Settings > Content > Lyrics ("Lyrics on Android Auto"), on by default. Co-Authored-By: Claude Opus 5.5 --- .../music/constants/PreferenceKeys.kt | 1 + .../music/playback/AndroidAutoLyrics.kt | 158 ++++++++++++++++++ .../music/playback/LyricsMetadataPlayer.kt | 100 +++++++++++ .../playback/MediaLibrarySessionCallback.kt | 110 +++++++++++- .../beatwave/music/playback/MusicService.kt | 40 ++++- .../ui/screens/settings/ContentSettings.kt | 23 +++ app/src/main/res/values/strings.xml | 4 + 7 files changed, 429 insertions(+), 7 deletions(-) create mode 100644 app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt create mode 100644 app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt diff --git a/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt b/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt index 2f2143ada..1d65a899d 100644 --- a/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt +++ b/app/src/main/kotlin/com/beatwave/music/constants/PreferenceKeys.kt @@ -602,6 +602,7 @@ val SwipeLyricsKey = booleanPreferencesKey("swipeLyrics") val EnableLyricsThumbnailPlayPauseKey = booleanPreferencesKey("enableLyricsThumbnailPlayPause") val LyricsTextPositionKey = stringPreferencesKey("lyricsTextPosition") val LyricsClickKey = booleanPreferencesKey("lyricsClick") +val AndroidAutoLyricsKey = booleanPreferencesKey("androidAutoLyrics") val LyricsScrollKey = booleanPreferencesKey("lyricsScrollKey") val LyricsRomanizeJapaneseKey = booleanPreferencesKey("lyricsRomanizeJapanese") val LyricsRomanizeKoreanKey = booleanPreferencesKey("lyricsRomanizeKorean") diff --git a/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt new file mode 100644 index 000000000..eca2f3486 --- /dev/null +++ b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt @@ -0,0 +1,158 @@ +/** + * BeatWave Project (C) 2026 + * Licensed under GPL-3.0 | See git history for contributors + */ + +package com.beatwave.music.playback + +import android.app.UiModeManager +import android.content.Context +import android.content.pm.PackageManager +import android.content.res.Configuration +import com.beatwave.music.constants.AndroidAutoLyricsKey +import com.beatwave.music.db.MusicDatabase +import com.beatwave.music.db.entities.LyricsEntity.Companion.LYRICS_NOT_FOUND +import com.beatwave.music.lyrics.LyricsEntry +import com.beatwave.music.lyrics.LyricsUtils +import com.beatwave.music.utils.dataStore +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch + +/** + * Synced lyrics for Android Auto. + * + * A media app can't draw its own screen in the car, Android Auto only renders + * what the media session publishes. So lyrics reach the car two ways: + * the current line is swapped into the now playing metadata (see + * [LyricsMetadataPlayer]), and the "Lyrics" browse tab lists a window of lines + * around it, where tapping a line seeks to it. + */ +class AndroidAutoLyrics( + private val context: Context, + private val scope: CoroutineScope, + private val database: MusicDatabase, + private val positionMs: () -> Long, + private val isPlaying: () -> Boolean, +) { + /** Fired on the main thread when the current line, the song or the setting changes. */ + var onChanged: () -> Unit = {} + + @Volatile + var enabled = false + private set + @Volatile + var songId: String? = null + private set + @Volatile + var lines: List = emptyList() + private set + @Volatile + var currentIndex = -1 + private set + + private val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + private val isAutomotive = context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) + + /** + * Android Auto puts the phone in car mode while projecting. Only then does the + * lyric line replace the artist, so the phone's own notification stays normal. + */ + @Volatile + var isInCar = false + private set + + private fun checkInCar() = + isAutomotive || uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_CAR + + /** The line to show in the now playing metadata for [mediaId], or null to leave it alone. */ + fun metadataLine(mediaId: String?): String? { + if (!enabled || mediaId == null || mediaId != songId || !isInCar) return null + return lines.getOrNull(currentIndex)?.text?.trim()?.takeIf { it.isNotEmpty() } + } + + @OptIn(ExperimentalCoroutinesApi::class) + fun start(songIds: Flow) { + scope.launch { + context.dataStore.data + .map { it[AndroidAutoLyricsKey] ?: true } + .distinctUntilChanged() + .collect { + enabled = it + onChanged() + } + } + + scope.launch { + songIds + .distinctUntilChanged() + .flatMapLatest { id -> + if (id == null) { + flowOf(null to emptyList()) + } else { + database.lyrics(id).map { id to parseSynced(it?.lyrics) } + } + }.collect { (id, parsed) -> + songId = id + lines = parsed + currentIndex = -1 + tick(force = true) + } + } + + // Car mode is a system call, so it's checked about once a second, not per metadata read. + scope.launch { + var sinceCarCheck = IDLE_MS + while (isActive) { + val ticking = enabled && lines.isNotEmpty() && isPlaying() + if (sinceCarCheck >= IDLE_MS) { + sinceCarCheck = 0 + val inCar = checkInCar() + if (inCar != isInCar) { + isInCar = inCar + onChanged() + } + } + if (ticking) tick(force = false) + val wait = if (ticking) TICK_MS else IDLE_MS + sinceCarCheck += wait + delay(wait) + } + } + } + + private fun tick(force: Boolean) { + val index = + if (lines.isEmpty()) -1 else LyricsUtils.findCurrentLineIndex(lines, positionMs()) + if (force || index != currentIndex) { + currentIndex = index + onChanged() + } + } + + /** Unsynced lyrics have no timestamps, so there's no current line to follow. */ + private fun parseSynced(raw: String?): List { + if (raw.isNullOrBlank() || raw == LYRICS_NOT_FOUND || !raw.trimStart().startsWith("[")) { + return emptyList() + } + return runCatching { LyricsUtils.parseLyrics(raw) } + .getOrDefault(emptyList()) + .filterNot { it.isBackground } + } + + companion object { + private const val TICK_MS = 200L + private const val IDLE_MS = 1000L + + /** Lines in the Lyrics tab: one before the current line and the rest after it. */ + const val TAB_LINES_BEFORE = 1 + const val TAB_LINES_TOTAL = 8 + } +} diff --git a/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt b/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt new file mode 100644 index 000000000..b3d0f16b4 --- /dev/null +++ b/app/src/main/kotlin/com/beatwave/music/playback/LyricsMetadataPlayer.kt @@ -0,0 +1,100 @@ +/** + * BeatWave Project (C) 2026 + * Licensed under GPL-3.0 | See git history for contributors + */ + +package com.beatwave.music.playback + +import androidx.media3.common.ForwardingPlayer +import androidx.media3.common.MediaMetadata +import androidx.media3.common.Player +import androidx.media3.common.util.UnstableApi +import java.util.concurrent.CopyOnWriteArraySet + +/** + * Wraps the player handed to the media session so the now playing metadata can + * carry the current lyric line, which is what Android Auto shows. + * Only the session sees this; the player itself and the in-app UI keep the real + * metadata. + * + * A short line takes the title row, with the song below it. A long one would get cut off, so it is + * split across the title and artist rows instead. + */ +@UnstableApi +class LyricsMetadataPlayer( + player: Player, + private val lyricLine: (mediaId: String?) -> String?, +) : ForwardingPlayer(player) { + private val listeners = CopyOnWriteArraySet() + private var publishedLine: String? = null + + override fun addListener(listener: Player.Listener) { + super.addListener(listener) + listeners += listener + } + + override fun removeListener(listener: Player.Listener) { + super.removeListener(listener) + listeners -= listener + } + + override fun getMediaMetadata(): MediaMetadata { + val metadata = super.getMediaMetadata() + val line = lyricLine(currentMediaItem?.mediaId) ?: return metadata + return metadata.withLyricLine(line) + } + + /** Pushes the metadata to the session again if the line changed since the last push. */ + fun invalidateLyricLine() { + val line = lyricLine(currentMediaItem?.mediaId) + if (line == publishedLine) return + publishedLine = line + val metadata = mediaMetadata + listeners.forEach { it.onMediaMetadataChanged(metadata) } + } + + private fun MediaMetadata.withLyricLine(line: String): MediaMetadata { + val builder = buildUpon() + if (line.length <= SINGLE_ROW_MAX_CHARS) { + // The title row is the big, bright one in the car, so the line goes + // there and the song moves down to the smaller row. + val song = listOfNotNull(displayTitle ?: title, artist) + .filter { it.isNotBlank() } + .joinToString(" · ") + builder + .setTitle(line) + .setDisplayTitle(line) + .setArtist(song) + .setSubtitle(song) + } else { + val split = splitPoint(line) + val top = line.substring(0, split).trim() + val bottom = line.substring(split).trim() + builder + .setTitle(top) + .setDisplayTitle(top) + .setArtist(bottom) + .setSubtitle(bottom) + } + return builder.build() + } + + /** The space closest to the middle, so both rows come out about the same length. */ + private fun splitPoint(line: String): Int { + val middle = line.length / 2 + val before = line.lastIndexOf(' ', middle) + val after = line.indexOf(' ', middle) + return when { + before < 0 && after < 0 -> middle + before < 0 -> after + after < 0 -> before + middle - before <= after - middle -> before + else -> after + } + } + + companion object { + /** About what fits on the artist row of a typical Android Auto screen. */ + private const val SINGLE_ROW_MAX_CHARS = 32 + } +} diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt index 85812e764..9372294eb 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt @@ -14,6 +14,7 @@ import androidx.core.net.toUri import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.MediaMetadata +import androidx.media3.common.Player import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.offline.Download import androidx.media3.session.LibraryResult @@ -73,6 +74,10 @@ constructor( var toggleLike: () -> Unit = {} var toggleStartRadio: () -> Unit = {} var toggleLibrary: () -> Unit = {} + var androidAutoLyrics: AndroidAutoLyrics? = null + + // Android Auto shows only this many root items as tabs. + private var rootChildrenLimit = Int.MAX_VALUE override fun onConnect( session: MediaSession, @@ -122,13 +127,17 @@ constructor( session: MediaLibrarySession, browser: MediaSession.ControllerInfo, params: MediaLibraryService.LibraryParams?, - ): ListenableFuture> = - Futures.immediateFuture( + ): ListenableFuture> { + params?.extras?.getInt(MediaConstants.EXTRAS_KEY_ROOT_CHILDREN_LIMIT, 0) + ?.takeIf { it > 0 } + ?.let { rootChildrenLimit = it } + return Futures.immediateFuture( LibraryResult.ofItem( rootMediaItem(), params.withContentStyleHints(), ), ) + } override fun onGetChildren( session: MediaLibrarySession, @@ -141,7 +150,9 @@ constructor( scope.future(Dispatchers.IO) { val children = when (parentId) { - MusicService.ROOT -> rootChildren() + MusicService.ROOT -> rootTabs() + + MusicService.LYRICS -> lyricsChildren() MusicService.SONG -> database.songsByCreateDateAsc().first() .map { it.toMediaItem(parentId) } @@ -388,8 +399,16 @@ constructor( mediaItems: MutableList, startIndex: Int, startPositionMs: Long, - ): ListenableFuture = - scope.future { + ): ListenableFuture { + val lyricPath = mediaItems.firstOrNull()?.mediaId?.split("/") + if (lyricPath?.firstOrNull() == MusicService.LYRICS) { + seekToLyricLine(mediaSession.player, lyricPath) + // Failing keeps the queue as it is; the tap was only a seek. + return Futures.immediateFailedFuture( + UnsupportedOperationException("Lyric lines seek, they don't replace the queue"), + ) + } + return scope.future { val defaultResult = MediaItemsWithStartPosition(emptyList(), startIndex, startPositionMs) val path = mediaItems.firstOrNull()?.mediaId?.split("/") ?: return@future defaultResult @@ -579,6 +598,7 @@ constructor( else -> defaultResult } } + } private fun drawableUri( @DrawableRes id: Int, @@ -638,6 +658,84 @@ constructor( ), ) + /** Root tabs, plus Lyrics when it's on. Artists gives way if the car can't show them all. */ + private fun rootTabs(): List { + if (androidAutoLyrics?.enabled != true) return rootChildren() + val tabs = (rootChildren() + lyricsTabMediaItem()).toMutableList() + if (tabs.size > rootChildrenLimit) tabs.removeAll { it.mediaId == MusicService.ARTIST } + return tabs.take(rootChildrenLimit) + } + + private fun lyricsTabMediaItem() = browsableMediaItem( + MusicService.LYRICS, + context.getString(R.string.lyrics), + null, + drawableUri(R.drawable.lyrics), + MediaMetadata.MEDIA_TYPE_FOLDER_MIXED, + ) + + /** + * A window of lines around the current one. It's rebuilt on every line change, + * so the current line stays near the top without the list needing to scroll. + */ + private fun lyricsChildren(): List { + val lyrics = androidAutoLyrics ?: return emptyList() + val songId = lyrics.songId + ?: return listOf(lyricsMessageItem(context.getString(R.string.android_auto_lyrics_no_song))) + if (lyrics.lines.isEmpty()) { + return listOf(lyricsMessageItem(context.getString(R.string.android_auto_lyrics_not_synced))) + } + + val current = lyrics.currentIndex.coerceAtLeast(0) + val from = (current - AndroidAutoLyrics.TAB_LINES_BEFORE).coerceAtLeast(0) + val to = (from + AndroidAutoLyrics.TAB_LINES_TOTAL).coerceAtMost(lyrics.lines.size) + return (from until to).map { index -> + val line = lyrics.lines[index] + val text = line.text.trim().ifEmpty { "♪" } + val isCurrent = index == lyrics.currentIndex + lyricLineMediaItem( + id = "${MusicService.LYRICS}/$songId/${line.time}", + title = if (isCurrent) "♪ $text" else text, + iconUri = if (isCurrent) drawableUri(R.drawable.play) else null, + // The subtitle row is dimmer, so the next line reads as "coming up". + subtitle = if (isCurrent) lyrics.lines.getOrNull(index + 1)?.text?.trim() else null, + ) + } + } + + private fun lyricsMessageItem(message: String) = + lyricLineMediaItem(MusicService.LYRICS + "/", message, drawableUri(R.drawable.lyrics)) + + private fun lyricLineMediaItem( + id: String, + title: String, + iconUri: Uri?, + subtitle: String? = null, + ) = MediaItem + .Builder() + .setMediaId(id) + .setMediaMetadata( + MediaMetadata + .Builder() + .setTitle(title) + .setSubtitle(subtitle) + .setArtworkUri(iconUri) + .setIsPlayable(true) + .setIsBrowsable(false) + .setMediaType(MediaMetadata.MEDIA_TYPE_MUSIC) + .setExtras(playableMediaItemExtras()) + .build(), + ).build() + + /** mediaId is lyrics//; only seeks if that song is still playing. */ + private fun seekToLyricLine(player: Player, path: List) { + val songId = path.getOrNull(1) ?: return + val timeMs = path.getOrNull(2)?.toLongOrNull() ?: return + if (player.currentMediaItem?.mediaId != songId) return + player.seekTo(timeMs) + if (!player.isPlaying) player.play() + } + private suspend fun getMediaItem(mediaId: String): MediaItem? { val path = mediaId.split("/") val type = path.firstOrNull() ?: return null @@ -645,6 +743,8 @@ constructor( return when (type) { MusicService.ROOT -> rootMediaItem() + MusicService.LYRICS -> lyricsTabMediaItem() + MusicService.SONG -> { if (path.size == 1) { rootChildren().first { it.mediaId == MusicService.SONG } diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt index 925a3f986..76ab71fc6 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt @@ -426,6 +426,10 @@ class MusicService : private var duckVolumeRestoreJob: Job? = null private lateinit var mediaSession: MediaLibrarySession + private lateinit var androidAutoLyrics: AndroidAutoLyrics + private lateinit var sessionPlayer: LyricsMetadataPlayer + private var lyricsTabRefreshJob: Job? = null + private var lastLyricsTabRefresh = 0L // Tracks if player has been properly initilized private val playerInitialized = MutableStateFlow(false) @@ -654,9 +658,18 @@ class MusicService : toggleStartRadio = ::toggleStartRadio toggleLibrary = ::toggleLibrary } + androidAutoLyrics = AndroidAutoLyrics( + context = this, + scope = scope, + database = database, + positionMs = { player.currentPosition }, + isPlaying = { player.isPlaying }, + ) + mediaLibrarySessionCallback.androidAutoLyrics = androidAutoLyrics + sessionPlayer = lyricsSessionPlayer(player) mediaSession = MediaLibrarySession - .Builder(this, player, mediaLibrarySessionCallback) + .Builder(this, sessionPlayer, mediaLibrarySessionCallback) .setSessionActivity( PendingIntent.getActivity( this, @@ -666,6 +679,11 @@ class MusicService : ), ).setBitmapLoader(CoilBitmapLoader(this, scope)) .build() + androidAutoLyrics.onChanged = { + sessionPlayer.invalidateLyricLine() + refreshLyricsTab() + } + androidAutoLyrics.start(currentMediaMetadata.map { it?.id }) player.repeatMode = dataStore.get(RepeatModeKey, REPEAT_MODE_OFF) // Restore shuffle mode if remember option is enabled @@ -1505,6 +1523,21 @@ class MusicService : updateWidgetUI(false) } + private fun lyricsSessionPlayer(player: Player) = + LyricsMetadataPlayer(player) { mediaId -> androidAutoLyrics.metadataLine(mediaId) } + + // Android Auto throttles browse refreshes, so fast lines are coalesced into one + // refresh at most every LYRICS_TAB_REFRESH_MS; the list is built when it's fetched. + private fun refreshLyricsTab() { + if (lyricsTabRefreshJob?.isActive == true) return + val wait = LYRICS_TAB_REFRESH_MS - (SystemClock.elapsedRealtime() - lastLyricsTabRefresh) + lyricsTabRefreshJob = scope.launch { + if (wait > 0) delay(wait) + lastLyricsTabRefresh = SystemClock.elapsedRealtime() + mediaSession.notifyChildrenChanged(LYRICS, Int.MAX_VALUE, null) + } + } + private fun updateNotification() { mediaSession.setCustomLayout( listOf( @@ -4454,7 +4487,8 @@ class MusicService : sleepTimer.player = player try { - (mediaSession as MediaSession).player = player + sessionPlayer = lyricsSessionPlayer(player) + (mediaSession as MediaSession).player = sessionPlayer } catch (e: Exception) { timber.log.Timber.e(e, "Failed to swap player in MediaSession") } @@ -4610,6 +4644,8 @@ class MusicService : const val PLAYLIST = "playlist" const val YOUTUBE_PLAYLIST = "youtube_playlist" const val SEARCH = "search" + const val LYRICS = "lyrics" + private const val LYRICS_TAB_REFRESH_MS = 500L const val SHUFFLE_ACTION = "__shuffle__" const val CHANNEL_ID = "music_channel_01" diff --git a/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt b/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt index 629f7693d..502b7272a 100644 --- a/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt +++ b/app/src/main/kotlin/com/beatwave/music/ui/screens/settings/ContentSettings.kt @@ -118,6 +118,7 @@ import com.beatwave.music.constants.ContentCountryKey import com.beatwave.music.constants.ContentLanguageKey import com.beatwave.music.constants.CountryCodeToName import com.beatwave.music.constants.DataSaverEnabledKey +import com.beatwave.music.constants.AndroidAutoLyricsKey import com.beatwave.music.constants.EnableBetterLyricsKey import com.beatwave.music.constants.EnableKugouKey import com.beatwave.music.constants.EnableLrcLibKey @@ -237,6 +238,7 @@ fun ContentSettings( val (enableKugou, onEnableKugouChange) = rememberPreference(key = EnableKugouKey, defaultValue = true) val (enableLrclib, onEnableLrclibChange) = rememberPreference(key = EnableLrcLibKey, defaultValue = true) val (enableBetterLyrics, onEnableBetterLyricsChange) = rememberPreference(key = EnableBetterLyricsKey, defaultValue = true) + val (androidAutoLyrics, onAndroidAutoLyricsChange) = rememberPreference(key = AndroidAutoLyricsKey, defaultValue = true) val (enableSimpMusic, onEnableSimpMusicChange) = rememberPreference(key = EnableSimpMusicKey, defaultValue = true) val (enableYouLyPlus, onEnableYouLyPlusChange) = rememberPreference(key = EnableYouLyPlusKey, defaultValue = true) val (enablePaxsenix, onEnablePaxsenixChange) = rememberPreference(key = EnablePaxsenixKey, defaultValue = true) @@ -1189,6 +1191,27 @@ fun ContentSettings( Material3SettingsGroup( title = stringResource(R.string.lyrics), items = listOf( + Material3SettingsItem( + icon = painterResource(R.drawable.lyrics), + title = { Text(stringResource(R.string.android_auto_lyrics)) }, + description = { Text(stringResource(R.string.android_auto_lyrics_desc)) }, + trailingContent = { + Switch( + checked = androidAutoLyrics, + onCheckedChange = onAndroidAutoLyricsChange, + thumbContent = { + Icon( + painter = painterResource( + id = if (androidAutoLyrics) R.drawable.check else R.drawable.close + ), + contentDescription = null, + modifier = Modifier.size(SwitchDefaults.IconSize) + ) + } + ) + }, + onClick = { onAndroidAutoLyricsChange(!androidAutoLyrics) } + ), Material3SettingsItem( icon = painterResource(R.drawable.lyrics), title = { Text(stringResource(R.string.enable_lrclib)) }, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b387dd3fc..4955f5c7f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -435,6 +435,10 @@ When this option is on, screenshots and the app\'s view in Recents are disabled. Enable LrcLib lyrics provider Enable KuGou lyrics provider + Lyrics on Android Auto + Show the current line on the car screen and add a Lyrics tab. Tap a line to jump to it + Nothing is playing + No synced lyrics for this song Hide explicit content Backup and restore From a4f9015b0ae1bd08947824ca1c864526e618dabc Mon Sep 17 00:00:00 2001 From: subscriptions-dot <252130413+subscriptions-dot@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:32:43 +0530 Subject: [PATCH 2/3] fix(auto): make car lyrics work on a real head unit Tested on the Desktop Head Unit with a phone running Android Auto: - Detect the car by the Android Auto controller connecting to the session. Current Android Auto no longer puts the phone in car mode, so the lyric line never showed in a real car. - Lyrics tab: one row, the current line as title and the next line as the subtitle (Android Auto draws it smaller and dimmer). No icon or marker. - In the car the radio button becomes a lyrics on/off button; the phone keeps the radio button. - Read the setting before the first tab request, and refresh the tabs when it changes, so the Lyrics tab doesn't go missing on a cold start. - Status rows ("Nothing is playing") aren't playable, so Android Auto doesn't offer them as "For you" suggestions. Co-Authored-By: Claude Opus 5.5 --- .../music/constants/MediaSessionConstants.kt | 2 + .../music/playback/AndroidAutoLyrics.kt | 50 ++++++++++++----- .../playback/MediaLibrarySessionCallback.kt | 55 +++++++++++++------ .../beatwave/music/playback/MusicService.kt | 40 ++++++++++++-- app/src/main/res/values/strings.xml | 2 + 5 files changed, 113 insertions(+), 36 deletions(-) diff --git a/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt b/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt index 1ff54e50c..c3a0b8f97 100644 --- a/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt +++ b/app/src/main/kotlin/com/beatwave/music/constants/MediaSessionConstants.kt @@ -14,9 +14,11 @@ object MediaSessionConstants { const val ACTION_TOGGLE_LIKE = "TOGGLE_LIKE" const val ACTION_TOGGLE_SHUFFLE = "TOGGLE_SHUFFLE" const val ACTION_TOGGLE_REPEAT_MODE = "TOGGLE_REPEAT_MODE" + const val ACTION_TOGGLE_CAR_LYRICS = "TOGGLE_CAR_LYRICS" val CommandToggleLibrary = SessionCommand(ACTION_TOGGLE_LIBRARY, Bundle.EMPTY) val CommandToggleLike = SessionCommand(ACTION_TOGGLE_LIKE, Bundle.EMPTY) val CommandToggleStartRadio = SessionCommand(ACTION_TOGGLE_START_RADIO, Bundle.EMPTY) val CommandToggleShuffle = SessionCommand(ACTION_TOGGLE_SHUFFLE, Bundle.EMPTY) val CommandToggleRepeatMode = SessionCommand(ACTION_TOGGLE_REPEAT_MODE, Bundle.EMPTY) + val CommandToggleCarLyrics = SessionCommand(ACTION_TOGGLE_CAR_LYRICS, Bundle.EMPTY) } diff --git a/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt index eca2f3486..be076281b 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/AndroidAutoLyrics.kt @@ -15,6 +15,7 @@ import com.beatwave.music.db.entities.LyricsEntity.Companion.LYRICS_NOT_FOUND import com.beatwave.music.lyrics.LyricsEntry import com.beatwave.music.lyrics.LyricsUtils import com.beatwave.music.utils.dataStore +import com.beatwave.music.utils.get import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay @@ -25,6 +26,7 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +import java.util.concurrent.atomic.AtomicInteger /** * Synced lyrics for Android Auto. @@ -32,8 +34,8 @@ import kotlinx.coroutines.launch * A media app can't draw its own screen in the car, Android Auto only renders * what the media session publishes. So lyrics reach the car two ways: * the current line is swapped into the now playing metadata (see - * [LyricsMetadataPlayer]), and the "Lyrics" browse tab lists a window of lines - * around it, where tapping a line seeks to it. + * [LyricsMetadataPlayer]), and the "Lyrics" browse tab shows it with the next + * line below, where tapping it seeks to the start of the line. */ class AndroidAutoLyrics( private val context: Context, @@ -62,15 +64,38 @@ class AndroidAutoLyrics( private val isAutomotive = context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) /** - * Android Auto puts the phone in car mode while projecting. Only then does the - * lyric line replace the artist, so the phone's own notification stays normal. + * Only in the car does the lyric line replace the song info, so the phone's own + * notification stays normal. Newer Android Auto versions don't put the phone in + * car mode, so a connected Android Auto controller counts too. */ @Volatile var isInCar = false private set + private val carControllers = AtomicInteger(0) + + /** Called by the session callback when Android Auto connects to or leaves the session. */ + fun onCarControllerConnected(connected: Boolean) { + if (connected) { + carControllers.incrementAndGet() + } else { + carControllers.updateAndGet { (it - 1).coerceAtLeast(0) } + } + scope.launch { updateInCar() } + } + private fun checkInCar() = - isAutomotive || uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_CAR + isAutomotive || + carControllers.get() > 0 || + uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_CAR + + private fun updateInCar() { + val inCar = checkInCar() + if (inCar != isInCar) { + isInCar = inCar + onChanged() + } + } /** The line to show in the now playing metadata for [mediaId], or null to leave it alone. */ fun metadataLine(mediaId: String?): String? { @@ -80,6 +105,10 @@ class AndroidAutoLyrics( @OptIn(ExperimentalCoroutinesApi::class) fun start(songIds: Flow) { + // Read once up front: Android Auto can ask for the tabs before the flow below + // first emits, and would get a tab list without Lyrics. + enabled = context.dataStore.get(AndroidAutoLyricsKey, true) + scope.launch { context.dataStore.data .map { it[AndroidAutoLyricsKey] ?: true } @@ -114,11 +143,7 @@ class AndroidAutoLyrics( val ticking = enabled && lines.isNotEmpty() && isPlaying() if (sinceCarCheck >= IDLE_MS) { sinceCarCheck = 0 - val inCar = checkInCar() - if (inCar != isInCar) { - isInCar = inCar - onChanged() - } + updateInCar() } if (ticking) tick(force = false) val wait = if (ticking) TICK_MS else IDLE_MS @@ -151,8 +176,7 @@ class AndroidAutoLyrics( private const val TICK_MS = 200L private const val IDLE_MS = 1000L - /** Lines in the Lyrics tab: one before the current line and the rest after it. */ - const val TAB_LINES_BEFORE = 1 - const val TAB_LINES_TOTAL = 8 + /** Package of the Android Auto app, which connects to the session while projecting. */ + const val ANDROID_AUTO_PACKAGE = "com.google.android.projection.gearhead" } } diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt index 9372294eb..2bcf441e5 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MediaLibrarySessionCallback.kt @@ -11,6 +11,7 @@ import android.net.Uri import android.os.Bundle import androidx.annotation.DrawableRes import androidx.core.net.toUri +import androidx.datastore.preferences.core.edit import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.MediaMetadata @@ -36,6 +37,7 @@ import com.music.innertube.models.SongItem import com.music.innertube.models.filterExplicit import com.music.innertube.models.filterVideoSongs import com.beatwave.music.R +import com.beatwave.music.constants.AndroidAutoLyricsKey import com.beatwave.music.constants.HideExplicitKey import com.beatwave.music.constants.HideVideoSongsKey import com.beatwave.music.constants.DataSaverEnabledKey @@ -58,6 +60,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.guava.future +import kotlinx.coroutines.launch import kotlinx.coroutines.plus import javax.inject.Inject @@ -84,6 +87,9 @@ constructor( controller: MediaSession.ControllerInfo, ): MediaSession.ConnectionResult { val connectionResult = super.onConnect(session, controller) + if (controller.packageName == AndroidAutoLyrics.ANDROID_AUTO_PACKAGE) { + androidAutoLyrics?.onCarControllerConnected(true) + } return MediaSession.ConnectionResult.accept( connectionResult.availableSessionCommands .buildUpon() @@ -92,11 +98,21 @@ constructor( .add(MediaSessionConstants.CommandToggleLibrary) .add(MediaSessionConstants.CommandToggleShuffle) .add(MediaSessionConstants.CommandToggleRepeatMode) + .add(MediaSessionConstants.CommandToggleCarLyrics) .build(), connectionResult.availablePlayerCommands, ) } + override fun onDisconnected( + session: MediaSession, + controller: MediaSession.ControllerInfo, + ) { + if (controller.packageName == AndroidAutoLyrics.ANDROID_AUTO_PACKAGE) { + androidAutoLyrics?.onCarControllerConnected(false) + } + } + override fun onCustomCommand( session: MediaSession, controller: MediaSession.ControllerInfo, @@ -111,6 +127,7 @@ constructor( !session.player.shuffleModeEnabled MediaSessionConstants.ACTION_TOGGLE_REPEAT_MODE -> session.player.toggleRepeatMode() + MediaSessionConstants.ACTION_TOGGLE_CAR_LYRICS -> toggleCarLyrics() } return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) } @@ -675,8 +692,8 @@ constructor( ) /** - * A window of lines around the current one. It's rebuilt on every line change, - * so the current line stays near the top without the list needing to scroll. + * One row: the current line as the title and the next one as the subtitle, which + * Android Auto draws smaller and dimmer. It's rebuilt on every line change. */ private fun lyricsChildren(): List { val lyrics = androidAutoLyrics ?: return emptyList() @@ -686,31 +703,29 @@ constructor( return listOf(lyricsMessageItem(context.getString(R.string.android_auto_lyrics_not_synced))) } - val current = lyrics.currentIndex.coerceAtLeast(0) - val from = (current - AndroidAutoLyrics.TAB_LINES_BEFORE).coerceAtLeast(0) - val to = (from + AndroidAutoLyrics.TAB_LINES_TOTAL).coerceAtMost(lyrics.lines.size) - return (from until to).map { index -> - val line = lyrics.lines[index] - val text = line.text.trim().ifEmpty { "♪" } - val isCurrent = index == lyrics.currentIndex + val index = lyrics.currentIndex.coerceAtLeast(0) + val line = lyrics.lines[index] + return listOf( lyricLineMediaItem( id = "${MusicService.LYRICS}/$songId/${line.time}", - title = if (isCurrent) "♪ $text" else text, - iconUri = if (isCurrent) drawableUri(R.drawable.play) else null, - // The subtitle row is dimmer, so the next line reads as "coming up". - subtitle = if (isCurrent) lyrics.lines.getOrNull(index + 1)?.text?.trim() else null, - ) - } + title = line.text.trim().ifEmpty { "♪" }, + iconUri = null, + subtitle = lyrics.lines.getOrNull(index + 1)?.text?.trim(), + ), + ) } + // Not playable: Android Auto offers playable items as "For you" suggestions on the + // home screen, and a status message has no business there. private fun lyricsMessageItem(message: String) = - lyricLineMediaItem(MusicService.LYRICS + "/", message, drawableUri(R.drawable.lyrics)) + lyricLineMediaItem(MusicService.LYRICS + "/", message, drawableUri(R.drawable.lyrics), playable = false) private fun lyricLineMediaItem( id: String, title: String, iconUri: Uri?, subtitle: String? = null, + playable: Boolean = true, ) = MediaItem .Builder() .setMediaId(id) @@ -720,13 +735,19 @@ constructor( .setTitle(title) .setSubtitle(subtitle) .setArtworkUri(iconUri) - .setIsPlayable(true) + .setIsPlayable(playable) .setIsBrowsable(false) .setMediaType(MediaMetadata.MEDIA_TYPE_MUSIC) .setExtras(playableMediaItemExtras()) .build(), ).build() + private fun toggleCarLyrics() { + scope.launch { + context.dataStore.edit { it[AndroidAutoLyricsKey] = !(it[AndroidAutoLyricsKey] ?: true) } + } + } + /** mediaId is lyrics//; only seeks if that song is still playing. */ private fun seekToLyricLine(player: Player, path: List) { val songId = path.getOrNull(1) ?: return diff --git a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt index 76ab71fc6..1233107d5 100644 --- a/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt +++ b/app/src/main/kotlin/com/beatwave/music/playback/MusicService.kt @@ -129,6 +129,7 @@ import com.beatwave.music.constants.ListenBrainzEnabledKey import com.beatwave.music.constants.ListenBrainzTokenKey import com.beatwave.music.constants.HistoryDuration import com.beatwave.music.constants.LastFMUseNowPlaying +import com.beatwave.music.constants.MediaSessionConstants.CommandToggleCarLyrics import com.beatwave.music.constants.MediaSessionConstants.CommandToggleLike import com.beatwave.music.constants.MediaSessionConstants.CommandToggleRepeatMode import com.beatwave.music.constants.MediaSessionConstants.CommandToggleShuffle @@ -428,6 +429,7 @@ class MusicService : private lateinit var mediaSession: MediaLibrarySession private lateinit var androidAutoLyrics: AndroidAutoLyrics private lateinit var sessionPlayer: LyricsMetadataPlayer + private var lastCarButtonState: Pair? = null private var lyricsTabRefreshJob: Job? = null private var lastLyricsTabRefresh = 0L @@ -682,6 +684,16 @@ class MusicService : androidAutoLyrics.onChanged = { sessionPlayer.invalidateLyricLine() refreshLyricsTab() + // The last car button depends on these, so redraw the buttons when they flip. + val carButtonState = androidAutoLyrics.isInCar to androidAutoLyrics.enabled + if (carButtonState != lastCarButtonState) { + // The Lyrics tab comes and goes with the setting, so the car needs a fresh tab list. + if (carButtonState.second != lastCarButtonState?.second) { + mediaSession.notifyChildrenChanged(ROOT, Int.MAX_VALUE, null) + } + lastCarButtonState = carButtonState + updateNotification() + } } androidAutoLyrics.start(currentMediaMetadata.map { it?.id }) player.repeatMode = dataStore.get(RepeatModeKey, REPEAT_MODE_OFF) @@ -1538,6 +1550,27 @@ class MusicService : } } + /** + * In the car the radio button gives way to a lyrics on/off switch, since the car + * has no other way to reach that setting. The phone keeps the radio button. + */ + private fun lastButton(): CommandButton { + if (::androidAutoLyrics.isInitialized && androidAutoLyrics.isInCar) { + val on = androidAutoLyrics.enabled + return CommandButton.Builder() + .setDisplayName(getString(if (on) R.string.android_auto_lyrics_hide else R.string.android_auto_lyrics_show)) + .setIconResId(if (on) R.drawable.lyrics_active else R.drawable.lyrics) + .setSessionCommand(CommandToggleCarLyrics) + .build() + } + return CommandButton.Builder() + .setDisplayName(getString(R.string.start_radio)) + .setIconResId(R.drawable.radio) + .setSessionCommand(CommandToggleStartRadio) + .setEnabled(currentSong.value != null) + .build() + } + private fun updateNotification() { mediaSession.setCustomLayout( listOf( @@ -1584,12 +1617,7 @@ class MusicService : .setIconResId(if (player.shuffleModeEnabled) R.drawable.shuffle_on else R.drawable.shuffle) .setSessionCommand(CommandToggleShuffle) .build(), - CommandButton.Builder() - .setDisplayName(getString(R.string.start_radio)) - .setIconResId(R.drawable.radio) - .setSessionCommand(CommandToggleStartRadio) - .setEnabled(currentSong.value != null) - .build(), + lastButton(), ), ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4955f5c7f..329d36854 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -439,6 +439,8 @@ Show the current line on the car screen and add a Lyrics tab. Tap a line to jump to it Nothing is playing No synced lyrics for this song + Show lyrics + Hide lyrics Hide explicit content Backup and restore From 209d4ec29248f601135818e106de848ab9f6dca5 Mon Sep 17 00:00:00 2001 From: subscriptions-dot <252130413+subscriptions-dot@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:32:43 +0530 Subject: [PATCH 3/3] fix(playback): don't hang forever waiting on the web PoToken With a signed-in account, every stream resolution also fetches WEB_REMIX metadata, which generates a PoToken in a WebView. When that WebView stops answering (seen when Android Auto starts the service with no activity), the song stayed in BUFFERING forever, and because the generator holds webPoTokenGenLock, every later song got stuck too until the app was restarted. - PoTokenGenerator: limit generation to 10 s; after a timeout the next call starts from a fresh WebView. - YTPlayerUtils: coroutineScope waited for the metadata job even after its 400 ms timeout dropped the result. Cancel it instead, and make the PoToken call interruptible so the cancel takes effect. Co-Authored-By: Claude Opus 5.5 --- .../com/beatwave/music/utils/YTPlayerUtils.kt | 14 +++++++++-- .../music/utils/potoken/PoTokenGenerator.kt | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt b/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt index 9b3474c96..4020b0a7d 100644 --- a/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt +++ b/app/src/main/kotlin/com/beatwave/music/utils/YTPlayerUtils.kt @@ -67,6 +67,7 @@ import java.net.URI import java.io.IOException import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.runInterruptible /** Higher first. Ranks Tidal search-result candidates so the matcher below tries * the best-quality candidate first instead of Tidal's own (unordered w.r.t. @@ -963,7 +964,10 @@ object YTPlayerUtils { val metaSessionId = YouTube.dataSyncId if (METADATA_CLIENT.useWebPoTokens && metaSessionId != null) { try { - metaPoToken = poTokenGenerator.getWebClientPoToken(videoId, metaSessionId) + // Interruptible, so cancelling this job below really stops it. + metaPoToken = runInterruptible { + poTokenGenerator.getWebClientPoToken(videoId, metaSessionId) + } } catch (e: Exception) { Timber.tag(logTag).e(e, "Metadata PoToken generation failed") } @@ -982,7 +986,13 @@ object YTPlayerUtils { null } } - mainDeferred.await() to kotlinx.coroutines.withTimeoutOrNull(400L) { metaDeferred.await() } + val main = mainDeferred.await() + // coroutineScope waits for every child, so a metadata fetch that is still + // running would hold up playback. Past the timeout its result is dropped + // anyway, so cancel it. + val meta = kotlinx.coroutines.withTimeoutOrNull(400L) { metaDeferred.await() } + if (meta == null) metaDeferred.cancel() + main to meta } // Debug uploaded track response diff --git a/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt b/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt index 2c6de8c59..f625cde47 100644 --- a/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt +++ b/app/src/main/kotlin/com/beatwave/music/utils/potoken/PoTokenGenerator.kt @@ -3,10 +3,12 @@ package com.beatwave.music.utils.potoken import android.webkit.CookieManager import com.beatwave.music.utils.cipher.CipherDeobfuscator import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout import timber.log.Timber class PoTokenGenerator { @@ -20,6 +22,11 @@ class PoTokenGenerator { private var webPoTokenStreamingPot: String? = null private var webPoTokenGenerator: PoTokenWebView? = null + // Set after a timeout, so the next call starts from a fresh WebView instead of + // waiting on the one that stopped answering. + @Volatile + private var recreateNext = false + fun getWebClientPoToken(videoId: String, sessionId: String): PoTokenResult? { Timber.tag(TAG).d("getWebClientPoToken called: videoId=$videoId, sessionId=$sessionId") Timber.tag(TAG).d("WebView state: supported=$webViewSupported, badImpl=$webViewBadImpl") @@ -30,10 +37,22 @@ class PoTokenGenerator { return try { Timber.tag(TAG).d("Calling runBlocking to generate poToken...") - runBlocking { getWebClientPoToken(videoId, sessionId, forceRecreate = false) } + // The WebView can stop answering (for example when the service was started by + // Android Auto with no activity around). Without a limit this call blocked + // forever while holding webPoTokenGenLock, so the song never started and + // every later song got stuck behind it until the app was restarted. + runBlocking { + withTimeout(PO_TOKEN_TIMEOUT_MS) { + getWebClientPoToken(videoId, sessionId, forceRecreate = recreateNext) + } + }.also { recreateNext = false } } catch (e: Exception) { Timber.tag(TAG).e(e, "poToken generation exception: ${e.javaClass.simpleName}: ${e.message}") when (e) { + is TimeoutCancellationException -> { + recreateNext = true + null + } is BadWebViewException -> { Timber.tag(TAG).e(e, "Could not obtain poToken because WebView is broken") webViewBadImpl = true @@ -97,4 +116,8 @@ class PoTokenGenerator { return PoTokenResult(playerPot, streamingPot) } + + private companion object { + const val PO_TOKEN_TIMEOUT_MS = 10_000L + } }