mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-01-06 01:20:29 +05:30
Merge pull request #4564 from Bnyro/master
feat: infinite playing queue when auto-insert related streams enabled
This commit is contained in:
commit
caf2b41fca
@ -169,7 +169,13 @@ class OnlinePlayerService : LifecycleService() {
|
|||||||
// clear the queue if it shouldn't be kept explicitly
|
// clear the queue if it shouldn't be kept explicitly
|
||||||
if (!keepQueue) PlayingQueue.clear()
|
if (!keepQueue) PlayingQueue.clear()
|
||||||
|
|
||||||
if (PlayingQueue.isEmpty()) updateQueue()
|
if (PlayingQueue.isEmpty()) {
|
||||||
|
PlayingQueue.updateQueue(streams!!.toStreamItem(videoId), playlistId, channelId)
|
||||||
|
insertRelatedStreamsToQueue()
|
||||||
|
}
|
||||||
|
else if (PlayingQueue.isLast() && playlistId == null && channelId == null) {
|
||||||
|
insertRelatedStreamsToQueue()
|
||||||
|
}
|
||||||
|
|
||||||
// save the current stream to the queue
|
// save the current stream to the queue
|
||||||
streams?.toStreamItem(videoId)?.let {
|
streams?.toStreamItem(videoId)?.let {
|
||||||
@ -347,19 +353,10 @@ class OnlinePlayerService : LifecycleService() {
|
|||||||
player?.checkForSegments(this, segments, sponsorBlockConfig)
|
player?.checkForSegments(this, segments, sponsorBlockConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateQueue() {
|
private fun insertRelatedStreamsToQueue() {
|
||||||
if (playlistId != null) {
|
if (!PlayerHelper.autoInsertRelatedVideos) return
|
||||||
streams?.toStreamItem(videoId)?.let {
|
streams?.relatedStreams?.toTypedArray()?.let {
|
||||||
PlayingQueue.insertPlaylist(playlistId!!, it)
|
PlayingQueue.add(*it, skipExisting = true)
|
||||||
}
|
|
||||||
} else if (channelId != null) {
|
|
||||||
streams?.toStreamItem(videoId)?.let {
|
|
||||||
PlayingQueue.insertChannel(channelId!!, it)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
streams?.relatedStreams?.toTypedArray()?.let {
|
|
||||||
if (PlayerHelper.autoInsertRelatedVideos) PlayingQueue.add(*it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -711,22 +711,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PlayingQueue.isEmpty()) {
|
val isFirstVideo = PlayingQueue.isEmpty()
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
if (isFirstVideo) {
|
||||||
if (playlistId != null) {
|
PlayingQueue.updateQueue(streams.toStreamItem(videoId), playlistId, channelId)
|
||||||
PlayingQueue.insertPlaylist(playlistId!!, streams.toStreamItem(videoId))
|
|
||||||
} else if (channelId != null) {
|
|
||||||
PlayingQueue.insertChannel(channelId!!, streams.toStreamItem(videoId))
|
|
||||||
} else {
|
|
||||||
PlayingQueue.updateCurrent(streams.toStreamItem(videoId))
|
|
||||||
if (PlayerHelper.autoInsertRelatedVideos) {
|
|
||||||
PlayingQueue.add(*streams.relatedStreams.toTypedArray())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
PlayingQueue.updateCurrent(streams.toStreamItem(videoId))
|
PlayingQueue.updateCurrent(streams.toStreamItem(videoId))
|
||||||
}
|
}
|
||||||
|
val isLastVideo = !isFirstVideo && PlayingQueue.isLast()
|
||||||
|
val isAutoQueue = playlistId == null && channelId == null
|
||||||
|
if (PlayerHelper.autoInsertRelatedVideos && (isFirstVideo || isLastVideo) && isAutoQueue) {
|
||||||
|
PlayingQueue.add(*streams.relatedStreams.toTypedArray(), skipExisting = true)
|
||||||
|
}
|
||||||
|
|
||||||
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false)) {
|
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false)) {
|
||||||
val videoStream = streams.videoStreams.firstOrNull()
|
val videoStream = streams.videoStreams.firstOrNull()
|
||||||
|
@ -24,9 +24,14 @@ object PlayingQueue {
|
|||||||
|
|
||||||
fun clear() = queue.clear()
|
fun clear() = queue.clear()
|
||||||
|
|
||||||
fun add(vararg streamItem: StreamItem) {
|
/**
|
||||||
|
* @param skipExisting Whether to skip the [streamItem] if it's already part of the queue
|
||||||
|
*/
|
||||||
|
fun add(vararg streamItem: StreamItem, skipExisting: Boolean = false) {
|
||||||
for (stream in streamItem) {
|
for (stream in streamItem) {
|
||||||
if (currentStream?.url?.toID() == stream.url?.toID() || stream.title.isNullOrBlank()) continue
|
if (skipExisting && contains(stream)) continue
|
||||||
|
if (currentStream?.url?.toID() == stream.url?.toID() ||
|
||||||
|
stream.title.isNullOrBlank()) continue
|
||||||
// remove if already present
|
// remove if already present
|
||||||
queue.remove(stream)
|
queue.remove(stream)
|
||||||
queue.add(stream)
|
queue.add(stream)
|
||||||
@ -68,6 +73,8 @@ object PlayingQueue {
|
|||||||
|
|
||||||
fun size() = queue.size
|
fun size() = queue.size
|
||||||
|
|
||||||
|
fun isLast() = currentIndex() == size() - 1
|
||||||
|
|
||||||
fun currentIndex(): Int = queue.indexOfFirst {
|
fun currentIndex(): Int = queue.indexOfFirst {
|
||||||
it.url?.toID() == currentStream?.url?.toID()
|
it.url?.toID() == currentStream?.url?.toID()
|
||||||
}.takeIf { it >= 0 } ?: 0
|
}.takeIf { it >= 0 } ?: 0
|
||||||
@ -159,7 +166,7 @@ object PlayingQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
|
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
|
||||||
scope.launch(Dispatchers.IO) {
|
scope.launch(Dispatchers.IO) {
|
||||||
runCatching {
|
runCatching {
|
||||||
val channel = RetrofitInstance.api.getChannel(channelId)
|
val channel = RetrofitInstance.api.getChannel(channelId)
|
||||||
@ -179,6 +186,12 @@ object PlayingQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateQueue(streamItem: StreamItem, playlistId: String?, channelId: String?) {
|
||||||
|
if (playlistId != null) insertPlaylist(playlistId, streamItem)
|
||||||
|
else if (channelId != null) insertChannel(channelId, streamItem)
|
||||||
|
else updateCurrent(streamItem)
|
||||||
|
}
|
||||||
|
|
||||||
fun onQueueItemSelected(index: Int) {
|
fun onQueueItemSelected(index: Int) {
|
||||||
try {
|
try {
|
||||||
val streamItem = queue[index]
|
val streamItem = queue[index]
|
||||||
|
Loading…
Reference in New Issue
Block a user