Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

YouTube Music: queued songs play next, video lists open at once, videos in search

Lelonio5bf203b17fda
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index 044bf01..9a2423e 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -284,6 +284,8 @@ class SquareApplication : Application(), ImageLoaderFactory {
/** YouTube Music; see [dev.lelonio.square.backend.youtube.YouTubeBackend]. */
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
val youtubeBackend: dev.lelonio.square.backend.youtube.YouTubeBackend by lazy {
+ // Songs already found for the videos in lists; see SongCounterparts.
+ dev.lelonio.square.backend.youtube.SongCounterparts.init(filesDir)
dev.lelonio.square.backend.youtube.YouTubeBackend(youtubeAccount)
}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt
index df7fefc..0c708d1 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/OfficialVideos.kt
@@ -24,6 +24,12 @@ object OfficialVideos {
/** Blocking: called from the stream resolver, on a loading thread. */
fun forSong(songId: String, song: StreamInfo): String? {
found[songId]?.let { return it.videoId }
+ // A song on YouTube Music is uploaded by the artist's "- Topic" channel.
+ // Anything else is already a video, and is its own picture: no search.
+ if (!song.uploaderName.orEmpty().endsWith(" - Topic")) {
+ found[songId] = Found(null)
+ return null
+ }
val title = song.name.orEmpty()
val artist = song.uploaderName.orEmpty().removeSuffix(" - Topic").trim()
val seconds = song.duration.toInt()
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
index f687db9..6930b6f 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/SkipFadePlayer.kt
@@ -22,6 +22,37 @@ class SkipFadePlayer(
fades.releaseMixer()
}
+ // "Play next" lands after the song playing and after whatever was queued
+ // before it, as on the Spotify side (PlayQueue.insertNext). ExoPlayer on its
+ // own appends, which put a queued song behind the rest of the playlist or
+ // the radio: queued, and never reached.
+
+ override fun addMediaItem(mediaItem: MediaItem) = addMediaItems(mutableListOf(mediaItem))
+
+ override fun addMediaItems(mediaItems: MutableList<MediaItem>) {
+ val at = if (mediaItems.isNotEmpty() && mediaItems.all(::isQueued)) queueEnd() else mediaItemCount
+ super.addMediaItems(at, mediaItems)
+ }
+
+ override fun addMediaItem(index: Int, mediaItem: MediaItem) = addMediaItems(index, mutableListOf(mediaItem))
+
+ override fun addMediaItems(index: Int, mediaItems: MutableList<MediaItem>) {
+ // The controller asks for the end; the queue knows better where that is.
+ val at = if (mediaItems.isNotEmpty() && mediaItems.all(::isQueued)) queueEnd() else index
+ super.addMediaItems(at, mediaItems)
+ }
+
+ private fun isQueued(item: MediaItem) =
+ item.mediaMetadata.extras?.getBoolean(dev.lelonio.square.ui.EXTRA_PLAY_NEXT) == true
+
+ /** Just past the run of queued songs that follows the one playing. */
+ private fun queueEnd(): Int {
+ if (mediaItemCount == 0) return 0
+ var at = currentMediaItemIndex + 1
+ while (at < mediaItemCount && isQueued(getMediaItemAt(at))) at++
+ return at
+ }
+
override fun seekToNext() = changing { super.seekToNext() }
override fun seekToNextMediaItem() = changing { super.seekToNextMediaItem() }
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/SongCounterparts.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/SongCounterparts.kt
new file mode 100644
index 0000000..cc65193
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/SongCounterparts.kt
@@ -0,0 +1,230 @@
+package dev.lelonio.square.backend.youtube
+
+import com.metrolist.innertube.YouTube
+import com.metrolist.innertube.models.Album
+import com.metrolist.innertube.models.Artist
+import com.metrolist.innertube.models.SongItem
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.SharedFlow
+import kotlinx.coroutines.flow.asSharedFlow
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.sync.Mutex
+import kotlinx.coroutines.sync.withLock
+import kotlinx.coroutines.withTimeoutOrNull
+import org.json.JSONArray
+import org.json.JSONObject
+import java.io.File
+import java.util.concurrent.ConcurrentHashMap
+
+/**
+ * The song a music video is a video of.
+ *
+ * A video saved to a list plays its own soundtrack: the intro, the skit in the
+ * middle, the sound effects, and a title no lyrics site knows. YouTube Music
+ * has the record as a separate entry, found here the way a listener would find
+ * it: the same title by the same artist, among the songs, and about as long.
+ *
+ * Shared by the lists, which show the song where they can, and by the stream
+ * resolver, which plays the song whatever the list had time to show — so a
+ * tap on a row is the right song even before its row has caught up. What is
+ * found is kept on disk: a playlist opened once opens converted ever after.
+ */
+object SongCounterparts {
+
+ private class Found(val song: SongItem?)
+
+ /** By video id. A null song is "looked, there is none", kept for this run only. */
+ private val found = ConcurrentHashMap<String, Found>()
+
+ /** One search per video at a time, whoever asks: a list and the resolver can ask at once. */
+ private val searching = ConcurrentHashMap<String, Mutex>()
+
+ /** The videos the lists have shown, by id: what the resolver needs to look for their songs. */
+ private val shown = ConcurrentHashMap<String, SongItem>()
+
+ private val _resolved = MutableSharedFlow<Pair<String, SongItem>>(extraBufferCapacity = 256)
+
+ /** Every song found, as (video id, song), as it is found. */
+ val resolved: SharedFlow<Pair<String, SongItem>> = _resolved.asSharedFlow()
+
+ private val io = CoroutineScope(SupervisorJob() + Dispatchers.IO)
+ private var file: File? = null
+ private val saveLock = Any()
+
+ fun init(dir: File) {
+ if (file != null) return
+ file = File(dir, FILE_NAME)
+ io.launch { load() }
+ }
+
+ fun isVideo(item: SongItem) =
+ !item.isEpisode && item.uploadEntityId == null &&
+ (item.musicVideoType == MUSIC_VIDEO_TYPE_OMV || item.musicVideoType == MUSIC_VIDEO_TYPE_UGC)
+
+ /** The song already found for [videoId], if any. */
+ fun known(videoId: String): SongItem? = found[videoId]?.song
+
+ /** The video [songId] was found for, if it was. */
+ fun videoOf(songId: String): String? = found.entries.firstOrNull { it.value.song?.id == songId }?.key
+
+ /**
+ * Whether [videoId] should play as its song: only a video one of the
+ * listener's own lists showed. The same video found anywhere else — a chart
+ * of videos, a search — is meant as the video, song known or not.
+ */
+ fun wanted(videoId: String): Boolean = shown.containsKey(videoId)
+
+ /** Remembers [items] as shown, so the resolver can find their songs if they are played. */
+ fun remember(items: List<SongItem>) {
+ items.filter(::isVideo).forEach { shown[it.id] = it }
+ }
+
+ suspend fun find(video: SongItem): SongItem? {
+ found[video.id]?.let { return it.song }
+ val lock = searching.computeIfAbsent(video.id) { Mutex() }
+ return lock.withLock {
+ found[video.id]?.let { return@withLock it.song }
+ search(video)
+ }
+ }
+
+ /** Blocking, for the resolver on its loading thread: [videoId] must have been [remember]ed. */
+ fun findBlocking(videoId: String): SongItem? {
+ found[videoId]?.let { return it.song }
+ val video = shown[videoId] ?: return null
+ return runBlocking { find(video) }
+ }
+
+ private suspend fun search(video: SongItem): SongItem? {
+ val artist = video.artists.firstOrNull()?.name.orEmpty()
+ val title = songTitleOf(video.title, artist)
+ var answered = false
+ val match = withTimeoutOrNull(TIMEOUT_MS) {
+ val results = YouTube.search("$title $artist".trim(), YouTube.SearchFilter.FILTER_SONG)
+ .getOrNull()?.items
+ answered = results != null
+ results.orEmpty()
+ .filterIsInstance<SongItem>()
+ .take(CANDIDATES)
+ .firstOrNull { isSongOf(video, title, it) }
+ }
+ // A timeout is not an answer: asked again next time.
+ if (match != null || answered) found[video.id] = Found(match)
+ if (match != null) {
+ _resolved.tryEmit(video.id to match)
+ save()
+ } else if (answered) {
+ android.util.Log.i(TAG, "no song for video ${video.id} \"${video.title}\"")
+ }
+ return match
+ }
+
+ /** A video's title as a song's: no "(Official Video)", no "Artist - " in front. */
+ private fun songTitleOf(title: String, artist: String): String {
+ var clean = title.replace(VIDEO_TAG, " ")
+ if (artist.isNotEmpty()) {
+ clean = clean.replace(Regex("^\\s*${Regex.escape(artist)}\\s*[-–—:|]\\s*", RegexOption.IGNORE_CASE), "")
+ }
+ return clean.replace(Regex("\\s+"), " ").trim().ifEmpty { title }
+ }
+
+ private fun isSongOf(video: SongItem, title: String, song: SongItem): Boolean {
+ if (song.musicVideoType != null && song.musicVideoType != MUSIC_VIDEO_TYPE_ATV) return false
+ val wanted = squash(title)
+ val got = squash(song.title)
+ if (wanted.isEmpty() || got.isEmpty()) return false
+ val sameTitle = wanted == got ||
+ (kotlin.math.min(wanted.length, got.length) >= 4 && (wanted.contains(got) || got.contains(wanted)))
+ if (!sameTitle) return false
+ val channel = video.artists.map { squash(it.name).replace(CHANNEL_NOISE, "") }
+ val sameArtist = song.artists.any { credited ->
+ val name = squash(credited.name)
+ name.isNotEmpty() && (channel.any { it == name || it.contains(name) } || squash(video.title).contains(name))
+ }
+ if (!sameArtist) return false
+ val a = video.duration
+ val b = song.duration
+ return a == null || b == null || kotlin.math.abs(a - b) <= DURATION_SLACK_S
+ }
+
+ private fun squash(text: String) = text.lowercase().filter { it.isLetterOrDigit() }
+
+ private fun load() {
+ val source = file?.takeIf { it.exists() } ?: return
+ runCatching {
+ val entries = JSONArray(source.readText())
+ for (i in 0 until entries.length()) {
+ val entry = entries.getJSONObject(i)
+ val video = entry.getString("v")
+ found.putIfAbsent(video, Found(songOf(entry.getJSONObject("s"))))
+ }
+ }.onFailure { android.util.Log.w(TAG, "counterparts unreadable", it) }
+ }
+
+ private fun save() {
+ val target = file ?: return
+ io.launch {
+ synchronized(saveLock) {
+ runCatching {
+ val entries = JSONArray()
+ found.forEach { (video, entry) ->
+ val song = entry.song ?: return@forEach
+ entries.put(JSONObject().put("v", video).put("s", jsonOf(song)))
+ }
+ val temp = File(target.parentFile, "${target.name}.tmp")
+ temp.writeText(entries.toString())
+ temp.renameTo(target)
+ }.onFailure { android.util.Log.w(TAG, "counterparts not saved", it) }
+ }
+ }
+ }
+
+ private fun jsonOf(song: SongItem) = JSONObject()
+ .put("id", song.id)
+ .put("title", song.title)
+ .put("artists", JSONArray().apply {
+ song.artists.forEach { put(JSONObject().put("n", it.name).put("i", it.id ?: "")) }
+ })
+ .put("album", song.album?.let { JSONObject().put("n", it.name).put("i", it.id) } ?: JSONObject.NULL)
+ .put("duration", song.duration ?: -1)
+ .put("thumb", song.thumbnail)
+ .put("explicit", song.explicit)
+
+ private fun songOf(json: JSONObject): SongItem {
+ val artists = json.getJSONArray("artists")
+ val album = json.optJSONObject("album")
+ return SongItem(
+ id = json.getString("id"),
+ title = json.getString("title"),
+ artists = (0 until artists.length()).map { i ->
+ val artist = artists.getJSONObject(i)
+ Artist(artist.getString("n"), artist.optString("i").takeIf { it.isNotEmpty() })
+ },
+ album = album?.let { Album(it.getString("n"), it.getString("i")) },
+ duration = json.optInt("duration", -1).takeIf { it > 0 },
+ musicVideoType = MUSIC_VIDEO_TYPE_ATV,
+ thumbnail = json.getString("thumb"),
+ explicit = json.optBoolean("explicit"),
+ )
+ }
+
+ private const val TAG = "SquareYouTube"
+ private const val FILE_NAME = "yt_counterparts.json"
+ private const val MUSIC_VIDEO_TYPE_OMV = "MUSIC_VIDEO_TYPE_OMV"
+ private const val MUSIC_VIDEO_TYPE_UGC = "MUSIC_VIDEO_TYPE_UGC"
+ private const val MUSIC_VIDEO_TYPE_ATV = "MUSIC_VIDEO_TYPE_ATV"
+ private const val TIMEOUT_MS = 5_000L
+ private const val CANDIDATES = 5
+
+ /** An intro or an outro the record does not have, at most. */
+ private const val DURATION_SLACK_S = 75
+ private val VIDEO_TAG = Regex(
+ "[(\\[][^)\\]]*(official|video|lyric|audio|visuali[sz]er|\\bmv\\b|\\bhd\\b|\\b4k\\b|clip)[^)\\]]*[)\\]]",
+ RegexOption.IGNORE_CASE,
+ )
+ private val CHANNEL_NOISE = Regex("vevo|topic|official|music|tv$")
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
index 4ea5b03..3dfd3bc 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt
@@ -22,6 +22,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.joinAll
import kotlinx.coroutines.withContext
import com.metrolist.innertube.YouTube
import com.metrolist.innertube.models.AlbumItem
@@ -111,6 +114,9 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
val playlists = async {
searchItems(trimmed, YouTube.SearchFilter.FILTER_COMMUNITY_PLAYLIST)
}
+ // Videos of every kind YouTube Music has: music videos, and the
+ // uploads people listen to rather than watch (ASMR, sessions).
+ val videos = async { searchItems(trimmed, YouTube.SearchFilter.FILTER_VIDEO) }
SearchResults(
tracks = songs.await().filterIsInstance<SongItem>().map(::toCatalogTrack),
@@ -131,6 +137,7 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
artworkUrl = artist.thumbnail?.atSize(COVER_SIZE),
)
},
+ videos = videos.await().filterIsInstance<SongItem>().map(::toVideoTrack),
playlists = playlists.await().filterIsInstance<PlaylistItem>().map { playlist ->
SearchItem(
uri = "$PLAYLIST_PREFIX${playlist.id}",
@@ -479,7 +486,12 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
else -> {
val listed = YouTube.playlist(id).getOrThrow().songs
- val songs = asSongs(listed)
+ // Only the listener's own lists: a video there is a song they
+ // saved, and they want the song. A list YouTube made of videos
+ // (a chart of music videos) is meant to be videos, and an album's
+ // entries are already its songs.
+ val own = id == LIKED_MUSIC_ID || id in editable
+ val songs = if (own) asSongs(listed) else listed
listed.zip(songs).forEach { (entry, song) ->
// What taking a song out of this list will need: YouTube
// removes an entry, not a song, and names the entry here —
@@ -494,82 +506,37 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
}
}
- /** What [songFor] found for a video, including that it found nothing. */
- private class Counterpart(val song: SongItem?)
-
- private val counterparts = java.util.concurrent.ConcurrentHashMap<String, Counterpart>()
+ /** Conversions run on past the list that asked for them; see [asSongs]. */
+ private val background = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.SupervisorJob() + Dispatchers.IO)
/**
- * [items] with every music video swapped for the song it is a video of.
+ * [items] with every music video swapped for the song it is a video of, as
+ * far as is known within [LIST_WAIT_MS]; see [SongCounterparts].
*
- * A video saved to a list plays its own soundtrack: the intro, the skit in
- * the middle, the sound effects, and a title no lyrics site knows. YouTube
- * Music has the record itself as a separate entry, and that is what should
- * play. Looked up a few at a time and given up on quickly, so a long list
- * opens at most a few seconds late the first time and at once after.
+ * The list does not wait for the rest: those keep being looked up, each
+ * row is put right as its song arrives (see [conversions]), and a row
+ * played before then plays its song anyway, because the resolver asks too.
*/
- private suspend fun asSongs(items: List<SongItem>): List<SongItem> = coroutineScope {
- val gate = kotlinx.coroutines.sync.Semaphore(COUNTERPART_PARALLELISM)
- items.map { item ->
- async {
- if (!isVideo(item)) item
- else gate.withPermit { songFor(item) } ?: item
+ private suspend fun asSongs(items: List<SongItem>): List<SongItem> {
+ SongCounterparts.remember(items)
+ val pending = items
+ .filter { SongCounterparts.isVideo(it) && SongCounterparts.known(it.id) == null }
+ .distinctBy { it.id }
+ if (pending.isNotEmpty()) {
+ val gate = kotlinx.coroutines.sync.Semaphore(COUNTERPART_PARALLELISM)
+ val jobs = pending.map { video ->
+ background.launch { gate.withPermit { SongCounterparts.find(video) } }
}
- }.awaitAll()
- }
-
- private fun isVideo(item: SongItem) =
- !item.isEpisode && item.uploadEntityId == null &&
- (item.musicVideoType == MUSIC_VIDEO_TYPE_OMV || item.musicVideoType == MUSIC_VIDEO_TYPE_UGC)
-
- private suspend fun songFor(video: SongItem): SongItem? {
- counterparts[video.id]?.let { return it.song }
- val artist = video.artists.firstOrNull()?.name.orEmpty()
- val title = songTitleOf(video.title, artist)
- var answered = false
- val found = kotlinx.coroutines.withTimeoutOrNull(COUNTERPART_TIMEOUT_MS) {
- val results = YouTube.search("$title $artist".trim(), YouTube.SearchFilter.FILTER_SONG)
- .getOrNull()?.items
- answered = results != null
- results.orEmpty()
- .filterIsInstance<SongItem>()
- .take(COUNTERPART_CANDIDATES)
- .firstOrNull { isSongOf(video, title, it) }
+ kotlinx.coroutines.withTimeoutOrNull(LIST_WAIT_MS) { jobs.joinAll() }
}
- // A timeout is not an answer: asked again next time.
- if (found != null || answered) counterparts[video.id] = Counterpart(found)
- if (found == null && answered) android.util.Log.i(TAG, "no song for video ${video.id} \"${video.title}\"")
- return found
- }
-
- /** A video's title as a song's: no "(Official Video)", no "Artist - " in front. */
- private fun songTitleOf(title: String, artist: String): String {
- var clean = title.replace(VIDEO_TAG, " ")
- if (artist.isNotEmpty()) {
- clean = clean.replace(Regex("^\\s*${Regex.escape(artist)}\\s*[-–—:|]\\s*", RegexOption.IGNORE_CASE), "")
+ return items.map { item ->
+ if (SongCounterparts.isVideo(item)) SongCounterparts.known(item.id) ?: item else item
}
- return clean.replace(Regex("\\s+"), " ").trim().ifEmpty { title }
}
- private fun isSongOf(video: SongItem, title: String, song: SongItem): Boolean {
- if (song.musicVideoType != null && song.musicVideoType != MUSIC_VIDEO_TYPE_ATV) return false
- val wanted = squash(title)
- val got = squash(song.title)
- if (wanted.isEmpty() || got.isEmpty()) return false
- val sameTitle = wanted == got || (kotlin.math.min(wanted.length, got.length) >= 4 && (wanted.contains(got) || got.contains(wanted)))
- if (!sameTitle) return false
- val channel = video.artists.map { squash(it.name).replace(CHANNEL_NOISE, "") }
- val sameArtist = song.artists.any { credited ->
- val name = squash(credited.name)
- name.isNotEmpty() && (channel.any { it == name || it.contains(name) } || squash(video.title).contains(name))
- }
- if (!sameArtist) return false
- val a = video.duration
- val b = song.duration
- return a == null || b == null || kotlin.math.abs(a - b) <= COUNTERPART_DURATION_SLACK_S
- }
-
- private fun squash(text: String) = text.lowercase().filter { it.isLetterOrDigit() }
+ /** Each song found for a video a list showed, as (the video's uri, the song). */
+ val conversions: kotlinx.coroutines.flow.Flow<Pair<String, CatalogTrack>> =
+ SongCounterparts.resolved.map { (video, song) -> "$TRACK_PREFIX$video" to toCatalogTrack(song) }
/**
* An artist's page as YouTube Music lays it out: their best-known songs, then
@@ -753,12 +720,14 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
YouTube.likeVideo(videoId, like = false).getOrThrow()
return@withContext
}
- val key = "$playlistId/$videoId"
+ // The list holds the video a song was shown in place of.
+ val listedId = SongCounterparts.videoOf(videoId) ?: videoId
+ val key = "$playlistId/$listedId"
// Read again when the list was not read here: the entry id is the
// only thing YouTube removes by.
if (entries[key] == null) tracksOf(playlistUri)
val entry = entries[key] ?: error("no entry for $videoId in $playlistId")
- YouTube.removeFromPlaylist(playlistId, videoId, entry).getOrThrow()
+ YouTube.removeFromPlaylist(playlistId, listedId, entry).getOrThrow()
entries.remove(key)
}
}
@@ -792,19 +761,10 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
private const val TAG = "SquareYouTube"
private const val HERO_SIZE = 1200
private val BYLINE_JOINERS = setOf("&", "e", "and", "y", "et", "und", "x", "i", "и", "ve", "और", "feat.", "ft.")
- private const val MUSIC_VIDEO_TYPE_OMV = "MUSIC_VIDEO_TYPE_OMV"
- private const val MUSIC_VIDEO_TYPE_UGC = "MUSIC_VIDEO_TYPE_UGC"
- private const val MUSIC_VIDEO_TYPE_ATV = "MUSIC_VIDEO_TYPE_ATV"
private const val COUNTERPART_PARALLELISM = 6
- private const val COUNTERPART_TIMEOUT_MS = 5_000L
- private const val COUNTERPART_CANDIDATES = 5
- /** An intro or an outro the record does not have, at most. */
- private const val COUNTERPART_DURATION_SLACK_S = 75
- private val VIDEO_TAG = Regex(
- "[(\\[][^)\\]]*(official|video|lyric|audio|visuali[sz]er|\\bmv\\b|\\bhd\\b|\\b4k\\b|clip)[^)\\]]*[)\\]]",
- RegexOption.IGNORE_CASE,
- )
- private val CHANNEL_NOISE = Regex("vevo|topic|official|music|tv$")
+
+ /** How long a list waits for its videos' songs before opening without them. */
+ private const val LIST_WAIT_MS = 1_500L
const val URI_SCHEME = "ytmusic:"
const val TRACK_PREFIX = "ytmusic:track:"
const val PLAYLIST_PREFIX = "ytmusic:playlist:"
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
index de64582..03b8034 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
@@ -1,6 +1,10 @@
package dev.lelonio.square.backend.youtube
import androidx.media3.common.Player
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.android.asCoroutineDispatcher
+import kotlinx.coroutines.launch
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.DefaultHttpDataSource
@@ -77,6 +81,11 @@ object YouTubePlayerFactory {
exo.addListener(object : Player.Listener {
override fun onMediaItemTransition(mediaItem: androidx.media3.common.MediaItem?, reason: Int) {
YouTubeVideoMode.onItem(mediaItem?.mediaId)
+ // A video whose song is already known says so at once.
+ mediaItem?.mediaId?.takeIf { it.startsWith(YouTubeBackend.TRACK_PREFIX) }
+ ?.let(YouTubeBackend::videoIdOfUri)
+ ?.takeIf(SongCounterparts::wanted)
+ ?.let { video -> SongCounterparts.known(video)?.let { retitle(exo, video, it) } }
// The next one asked for now, while this one plays.
val next = exo.nextMediaItemIndex
if (next >= 0) {
@@ -91,6 +100,13 @@ object YouTubePlayerFactory {
}
val preferences = (host.context.applicationContext as? dev.lelonio.square.SquareApplication)
?.preferences
+ // Each song found for a video in the queue renames its entry, so
+ // the player shows what it plays; see SongCounterparts.
+ CoroutineScope(SupervisorJob() + android.os.Handler(host.looper).asCoroutineDispatcher()).launch {
+ SongCounterparts.resolved.collect { (video, song) ->
+ if (SongCounterparts.wanted(video)) retitle(exo, video, song)
+ }
+ }
// The talking and the sketches around a music video; see SponsorBlock.
SponsorBlock(exo) { preferences?.sponsorBlock?.value ?: true }
// Every change of song the listener asks for goes through the
@@ -99,6 +115,32 @@ object YouTubePlayerFactory {
}
}
+ /**
+ * Shows [song] in place of [video] wherever the queue has it. Same stream
+ * address, so nothing reloads: only the name, the picture and the id the
+ * rest of the app reads (lyrics, credits, likes) change.
+ */
+ private fun retitle(player: ExoPlayer, video: String, song: com.metrolist.innertube.models.SongItem) {
+ val listed = "${YouTubeBackend.TRACK_PREFIX}$video"
+ for (index in 0 until player.mediaItemCount) {
+ val item = player.getMediaItemAt(index)
+ if (item.mediaId != listed) continue
+ val metadata = item.mediaMetadata.buildUpon()
+ .setTitle(song.title)
+ .setArtist(song.artists.joinToString(", ") { it.name })
+ .setAlbumTitle(song.album?.name ?: item.mediaMetadata.albumTitle)
+ .setArtworkUri(android.net.Uri.parse(song.thumbnail.atSize(COVER_SIZE)))
+ .build()
+ player.replaceMediaItem(
+ index,
+ item.buildUpon()
+ .setMediaId("${YouTubeBackend.TRACK_PREFIX}${song.id}")
+ .setMediaMetadata(metadata)
+ .build(),
+ )
+ }
+ }
+
/** Temporary: chasing a stall where the position stops with the sink idle. */
private object Diagnostics : androidx.media3.exoplayer.analytics.AnalyticsListener {
override fun onAudioUnderrun(
@@ -277,7 +319,16 @@ private class YouTubeStreamResolver(
val (videoId, info) = if (watching && base.startsWith(YouTubeBackend.TRACK_PREFIX)) {
YouTubeStreams.watchSource(songId)
} else {
- songId to YouTubeStreams.info(songId)
+ // A music video from a list, heard: its song, found now if the list
+ // did not get to it before the row was tapped; see SongCounterparts.
+ val heard = if (!watching && base.startsWith(YouTubeBackend.TRACK_PREFIX) &&
+ SongCounterparts.wanted(songId)
+ ) {
+ runCatching { SongCounterparts.findBlocking(songId) }.getOrNull()?.id
+ } else {
+ null
+ } ?: songId
+ heard to YouTubeStreams.info(heard)
}
// The picture alone, as sharp as is worth it on a phone: H.264 first,
diff --git a/app/src/main/java/dev/lelonio/square/data/SearchResults.kt b/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
index e21cc0d..411d7d5 100644
--- a/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SearchResults.kt
@@ -13,6 +13,8 @@ data class SearchResults(
val artists: List<SearchItem> = emptyList(),
val albums: List<SearchItem> = emptyList(),
val playlists: List<SearchItem> = emptyList(),
+ /** Videos, music and otherwise (ASMR, sessions); YouTube Music only. Played as videos. */
+ val videos: List<CatalogTrack> = emptyList(),
/**
* The tracks that were found by their words rather than their titles.
*
@@ -25,7 +27,7 @@ data class SearchResults(
) {
/** How many rows this holds in total, for telling a page from an echo. */
val count: Int
- get() = tracks.size + artists.size + albums.size + playlists.size
+ get() = tracks.size + artists.size + albums.size + playlists.size + videos.size
/**
* This, with another page's rows after it.
@@ -44,12 +46,14 @@ data class SearchResults(
artists = artists + page.artists.filter { it.uri !in heldArtists },
albums = albums + page.albums.filter { it.uri !in heldAlbums },
playlists = playlists + page.playlists.filter { it.uri !in heldLists },
+ videos = videos + page.videos.filter { video -> videos.none { it.uri == video.uri } },
lyricMatches = lyricMatches + page.lyricMatches,
)
}
val isEmpty: Boolean
- get() = tracks.isEmpty() && artists.isEmpty() && albums.isEmpty() && playlists.isEmpty()
+ get() = tracks.isEmpty() && artists.isEmpty() && albums.isEmpty() && playlists.isEmpty() &&
+ videos.isEmpty()
}
/**
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
index bdada47..1eda4a7 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
@@ -348,6 +348,14 @@ class MainActivity : ComponentActivity() {
if (wasEmpty) {
player.prepare()
player.play()
+ } else {
+ // Said, because nothing on screen changes: the song went somewhere
+ // the listener cannot see from here.
+ android.widget.Toast.makeText(
+ this,
+ getString(dev.lelonio.square.R.string.added_to_queue, track.name),
+ android.widget.Toast.LENGTH_SHORT,
+ ).show()
}
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index df491ea..36f1e4c 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -757,6 +757,17 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
private var devicesJob: Job? = null
init {
+ // A video in an open list, put right as its song is found; see
+ // YouTubeBackend.asSongs, which opens the list without waiting for all.
+ viewModelScope.launch {
+ container.youtubeBackend.conversions.collect { (videoUri, song) ->
+ val open = _playlist.value
+ if (open.tracks.none { it.uri == videoUri }) return@collect
+ _playlist.value = open.copy(
+ tracks = open.tracks.map { if (it.uri == videoUri) song else it },
+ )
+ }
+ }
// Offline and back, without a restart in between.
//
// The library and the home page are each built once, from whichever
diff --git a/app/src/main/java/dev/lelonio/square/ui/browse/NewScreen.kt b/app/src/main/java/dev/lelonio/square/ui/browse/NewScreen.kt
index d00462e..871c5e6 100644
--- a/app/src/main/java/dev/lelonio/square/ui/browse/NewScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/browse/NewScreen.kt
@@ -360,7 +360,7 @@ private fun SongRow(track: CatalogTrack, onMenu: () -> Unit, onClick: () -> Unit
Modifier
.fillMaxWidth()
.clip(shape)
- .pressable(onClick, shape = shape, pressedScale = 0.985f)
+ .pressable(onClick, shape = shape, pressedScale = 0.985f, onLongClick = onMenu)
.padding(vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
diff --git a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
index 945f882..8ee3797 100644
--- a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt
@@ -2806,7 +2806,8 @@ private fun TrackRow(
if (unavailable) {
Modifier
} else {
- Modifier.pressable(onClick, shape = shape, pressedScale = 0.985f)
+ // A long press opens the song's menu, the same as the dots.
+ Modifier.pressable(onClick, shape = shape, pressedScale = 0.985f, onLongClick = onMenu)
},
)
.graphicsLayer { alpha = dim }
diff --git a/app/src/main/java/dev/lelonio/square/ui/search/SearchScreen.kt b/app/src/main/java/dev/lelonio/square/ui/search/SearchScreen.kt
index be28b19..8db87a4 100644
--- a/app/src/main/java/dev/lelonio/square/ui/search/SearchScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/search/SearchScreen.kt
@@ -62,6 +62,7 @@ import com.adamglin.phosphoricons.regular.DotsThree
private enum class Kind(@StringRes val label: Int) {
ALL(R.string.feed_all),
TRACKS(R.string.tracks),
+ VIDEOS(R.string.videos),
ARTISTS(R.string.artists),
ALBUMS(R.string.albums),
PLAYLISTS(R.string.playlists),
@@ -150,7 +151,8 @@ fun SearchScreen(
if (!state.results.isEmpty && !state.loading) {
item(contentType = "chips") {
- KindRow(kind, backdrop) { kind = it }
+ // Videos only where the source has them: Spotify's search does not.
+ KindRow(kind, backdrop, hasVideos = state.results.videos.isNotEmpty()) { kind = it }
}
}
@@ -301,6 +303,30 @@ fun SearchScreen(
}
}
+ val videos = state.results.videos.let { if (all) it.take(TOP_RESULTS) else it }
+ if ((all || kind == Kind.VIDEOS) && videos.isNotEmpty()) {
+ item(contentType = "section") { SectionTitle(stringResource(R.string.videos)) }
+ items(
+ count = videos.size,
+ key = { "video-${videos[it].uri}-$it" },
+ contentType = { "track" },
+ ) { index ->
+ val video = videos[index]
+ SwipeToQueue(onQueue = { onEnqueue(video) }) {
+ ResultRow(
+ title = video.name,
+ subtitle = video.artist,
+ artworkUrl = video.artworkUrl,
+ highlighted = video.uri == nowPlayingUri,
+ round = false,
+ // Played as a video: the picture is what they are.
+ onClick = { onPlayTrack(videos, index) },
+ onMenu = { onTrackMenu(video) },
+ )
+ }
+ }
+ }
+
if (all || kind == Kind.ARTISTS) {
section(R.string.artists, state.results.artists, all, round = true, onOpenContext)
}
@@ -337,13 +363,13 @@ fun SearchScreen(
/** The same chips the home page filters with; see the note there. */
@Composable
-private fun KindRow(selected: Kind, backdrop: Backdrop, onSelect: (Kind) -> Unit) {
+private fun KindRow(selected: Kind, backdrop: Backdrop, hasVideos: Boolean, onSelect: (Kind) -> Unit) {
LazyRow(
contentPadding = PaddingValues(horizontal = 20.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.padding(top = 16.dp, bottom = 4.dp),
) {
- items(Kind.entries.toList(), key = { it.name }) { entry ->
+ items(Kind.entries.filter { it != Kind.VIDEOS || hasVideos }, key = { it.name }) { entry ->
dev.lelonio.square.ui.components.FilterChip(
label = stringResource(entry.label),
selected = entry == selected,
@@ -439,7 +465,7 @@ private fun ResultRow(
Row(
Modifier
.fillMaxWidth()
- .pressable(onClick, shape = RoundedCornerShape(16.dp), pressedScale = 0.98f)
+ .pressable(onClick, shape = RoundedCornerShape(16.dp), pressedScale = 0.98f, onLongClick = onMenu)
.padding(start = 24.dp, end = 12.dp, top = 8.dp, bottom = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index b48b95f..f7c79f8 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -477,4 +477,6 @@
<string name="rotate_video">Ins Vollbild drehen</string>
<string name="video_age_restricted">Das offizielle Video ist altersbeschränkt und erfordert eine YouTube-Anmeldung: Das Video des Songs wird gezeigt.</string>
<string name="video_unavailable">Das offizielle Video kann nicht abgespielt werden: Das Video des Songs wird gezeigt.</string>
+ <string name="added_to_queue">Zur Warteschlange hinzugefügt: %s</string>
+ <string name="videos">Videos</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 797806c..bab747c 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -483,4 +483,6 @@
<string name="rotate_video">Girar a pantalla completa</string>
<string name="video_age_restricted">El vídeo oficial tiene restricción de edad y requiere iniciar sesión en YouTube: se muestra el vídeo de la canción.</string>
<string name="video_unavailable">El vídeo oficial no se puede reproducir: se muestra el vídeo de la canción.</string>
+ <string name="added_to_queue">Añadido a la cola: %s</string>
+ <string name="videos">Vídeos</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 882f197..31bb8c6 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -477,4 +477,6 @@
<string name="rotate_video">Pivoter en plein écran</string>
<string name="video_age_restricted">La vidéo officielle est soumise à une limite d\'âge et nécessite une connexion à YouTube : affichage de la vidéo du titre.</string>
<string name="video_unavailable">La vidéo officielle ne peut pas être lue : affichage de la vidéo du titre.</string>
+ <string name="added_to_queue">Ajouté à la file d\'attente : %s</string>
+ <string name="videos">Vidéos</string>
</resources>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index 1d7837b..63cf17e 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -492,4 +492,6 @@
<string name="rotate_video">फ़ुल स्क्रीन के लिए घुमाएँ</string>
<string name="video_age_restricted">आधिकारिक वीडियो आयु-प्रतिबंधित है और YouTube साइन-इन चाहिए: गाने का वीडियो दिखाया जा रहा है।</string>
<string name="video_unavailable">आधिकारिक वीडियो नहीं चल सकता: गाने का वीडियो दिखाया जा रहा है।</string>
+ <string name="added_to_queue">कतार में जोड़ा गया: %s</string>
+ <string name="videos">वीडियो</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 1355ac8..5fd057e 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -478,4 +478,6 @@
<string name="rotate_video">Ruota a schermo intero</string>
<string name="video_age_restricted">Il video ufficiale è vietato ai minori e richiede l\'accesso a YouTube: mostro il video del brano.</string>
<string name="video_unavailable">Il video ufficiale non può essere riprodotto: mostro il video del brano.</string>
+ <string name="added_to_queue">Aggiunto in coda: %s</string>
+ <string name="videos">Video</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index b317f81..b1a3e09 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -477,4 +477,6 @@
<string name="rotate_video">Girar para tela cheia</string>
<string name="video_age_restricted">O vídeo oficial tem restrição de idade e exige login no YouTube: mostrando o vídeo da música.</string>
<string name="video_unavailable">O vídeo oficial não pode ser reproduzido: mostrando o vídeo da música.</string>
+ <string name="added_to_queue">Adicionado à fila: %s</string>
+ <string name="videos">Vídeos</string>
</resources>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index e02c6f1..73e0583 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -504,4 +504,6 @@
<string name="rotate_video">Повернуть на весь экран</string>
<string name="video_age_restricted">Официальное видео с возрастным ограничением и требует входа в YouTube: показано видео трека.</string>
<string name="video_unavailable">Официальное видео недоступно: показано видео трека.</string>
+ <string name="added_to_queue">Добавлено в очередь: %s</string>
+ <string name="videos">Видео</string>
</resources>
diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml
index 67ba4bf..c67eb88 100644
--- a/app/src/main/res/values-tr/strings.xml
+++ b/app/src/main/res/values-tr/strings.xml
@@ -490,4 +490,6 @@
<string name="rotate_video">Tam ekran için döndür</string>
<string name="video_age_restricted">Resmi video yaş kısıtlamalı ve YouTube girişi gerektiriyor: şarkının kendi videosu gösteriliyor.</string>
<string name="video_unavailable">Resmi video oynatılamıyor: şarkının kendi videosu gösteriliyor.</string>
+ <string name="added_to_queue">Sıraya eklendi: %s</string>
+ <string name="videos">Videolar</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index c38e8cb..05d66c1 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -504,4 +504,6 @@
<string name="rotate_video">Rotate to full screen</string>
<string name="video_age_restricted">The official video is age-restricted and needs a YouTube sign-in: showing the song\'s own video.</string>
<string name="video_unavailable">The official video can\'t be played: showing the song\'s own video.</string>
+ <string name="added_to_queue">Added to queue: %s</string>
+ <string name="videos">Videos</string>
</resources>