-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/zionhuang/music/playback/queues/LocalAlbumRadio.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.zionhuang.music.playback.queues | ||
|
||
import androidx.media3.common.MediaItem | ||
import com.zionhuang.innertube.YouTube | ||
import com.zionhuang.innertube.models.WatchEndpoint | ||
import com.zionhuang.music.db.entities.AlbumWithSongs | ||
import com.zionhuang.music.extensions.toMediaItem | ||
import com.zionhuang.music.models.MediaMetadata | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.withContext | ||
|
||
class LocalAlbumRadio( | ||
private val albumWithSongs: AlbumWithSongs, | ||
private val startIndex: Int = 0, | ||
) : Queue { | ||
override val preloadItem: MediaMetadata? = null | ||
|
||
private lateinit var playlistId: String | ||
private val endpoint: WatchEndpoint | ||
get() = WatchEndpoint( | ||
playlistId = playlistId, | ||
params = "wAEB" | ||
) | ||
|
||
private var continuation: String? = null | ||
private var firstTimeLoaded: Boolean = false | ||
|
||
override suspend fun getInitialStatus(): Queue.Status = withContext(IO) { | ||
Queue.Status( | ||
title = albumWithSongs.album.title, | ||
items = albumWithSongs.songs.map { it.toMediaItem() }, | ||
mediaItemIndex = startIndex | ||
) | ||
} | ||
|
||
override fun hasNextPage(): Boolean = !firstTimeLoaded || continuation != null | ||
|
||
override suspend fun nextPage(): List<MediaItem> = withContext(IO) { | ||
if (!firstTimeLoaded) { | ||
playlistId = YouTube.album(albumWithSongs.album.id).getOrThrow().album.playlistId | ||
val nextResult = YouTube.next(endpoint, continuation).getOrThrow() | ||
continuation = nextResult.continuation | ||
firstTimeLoaded = true | ||
return@withContext nextResult.items.subList(albumWithSongs.songs.size, nextResult.items.size).map { it.toMediaItem() } | ||
} | ||
val nextResult = YouTube.next(endpoint, continuation).getOrThrow() | ||
continuation = nextResult.continuation | ||
nextResult.items.map { it.toMediaItem() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters