improve playing queue

This commit is contained in:
Bnyro 2022-09-19 21:37:30 +02:00
parent 60b49f2120
commit 0db7821418
4 changed files with 41 additions and 38 deletions

View File

@ -696,6 +696,7 @@ class PlayerFragment : BaseFragment() {
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
}
} catch (e: Exception) {
e.printStackTrace()
}
}
@ -753,7 +754,7 @@ class PlayerFragment : BaseFragment() {
}
private fun playVideo() {
PlayingQueue.add(videoId!!)
PlayingQueue.updateCurrent(videoId!!)
lifecycleScope.launchWhenCreated {
streams = try {
RetrofitInstance.api.getStreams(videoId!!)
@ -876,7 +877,7 @@ class PlayerFragment : BaseFragment() {
private fun playNextVideo() {
if (nextStreamId == null) return
// check whether there is a new video in the queue
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId!!)
val nextQueueVideo = PlayingQueue.getNext()
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
// by making sure that the next and the current video aren't the same
saveWatchPosition()
@ -1106,7 +1107,7 @@ class PlayerFragment : BaseFragment() {
// next and previous buttons
playerBinding.skipPrev.visibility = if (
skipButtonsEnabled && PlayingQueue.queue.indexOf(videoId!!) != 0
skipButtonsEnabled && PlayingQueue.hasPrev()
) {
View.VISIBLE
} else {
@ -1115,8 +1116,7 @@ class PlayerFragment : BaseFragment() {
playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE
playerBinding.skipPrev.setOnClickListener {
val index = PlayingQueue.queue.indexOf(videoId!!) - 1
videoId = PlayingQueue.queue[index]
videoId = PlayingQueue.getPrev()
playVideo()
}

View File

@ -162,6 +162,8 @@ class BackgroundMode : Service() {
private fun playAudio(
seekToPosition: Long
) {
PlayingQueue.updateCurrent(videoId)
initializePlayer()
setMediaItem()
@ -247,7 +249,7 @@ class BackgroundMode : Service() {
*/
private fun playNextVideo() {
if (nextStreamId == null || nextStreamId == videoId) return
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId)
val nextQueueVideo = PlayingQueue.getNext()
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
// play new video on background

View File

@ -20,12 +20,8 @@ class AutoPlayHelper(
currentVideoId: String,
relatedStreams: List<StreamItem>?
): String? {
return if (PlayingQueue.queue.last() != currentVideoId) {
val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId)
PlayingQueue.queue[currentVideoIndex + 1]
} else if (playlistId == null) {
return if (playlistId == null) {
getNextTrendingVideoId(
currentVideoId,
relatedStreams
)
} else {
@ -39,21 +35,13 @@ class AutoPlayHelper(
* get the id of the next related video
*/
private fun getNextTrendingVideoId(
videoId: String,
relatedStreams: List<StreamItem>?
): String? {
// don't play a video if it got played before already
if (relatedStreams == null || relatedStreams.isEmpty()) return null
var index = 0
var nextStreamId: String? = null
while (nextStreamId == null ||
(
PlayingQueue.queue.contains(nextStreamId) &&
PlayingQueue.queue.indexOf(videoId) > PlayingQueue.queue.indexOf(
nextStreamId
)
)
) {
while (nextStreamId == null || PlayingQueue.containsBefore(nextStreamId)) {
nextStreamId = relatedStreams[index].url!!.toID()
if (index + 1 < relatedStreams.size) {
index += 1
@ -102,18 +90,4 @@ class AutoPlayHelper(
// return null when no nextPage is found
return null
}
/**
* get the videoId of the next video in the playing queue
*/
fun getNextPlayingQueueVideoId(
currentVideoId: String
): String? {
return if (PlayingQueue.queue.last() != currentVideoId) {
val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId)
PlayingQueue.queue[currentVideoIndex + 1]
} else {
null
}
}
}

View File

@ -1,8 +1,8 @@
package com.github.libretube.util
object PlayingQueue {
val queue = mutableListOf<String>()
val currentVideoId: String? = null
private val queue = mutableListOf<String>()
private var currentVideoId: String? = null
fun clear() {
queue.clear()
@ -21,7 +21,34 @@ object PlayingQueue {
fun getNext(): String? {
val currentIndex = queue.indexOf(currentVideoId)
return if (currentIndex > queue.size) null
else queue[currentIndex + 1]
return if (currentIndex > queue.size) {
null
} else {
queue[currentIndex + 1]
}
}
fun getPrev(): String {
return queue[
queue.indexOf(currentVideoId) - 1
]
}
fun hasPrev(): Boolean {
val currentIndex = queue.indexOf(currentVideoId)
return currentIndex >= 1
}
fun contains(videoId: String): Boolean {
return queue.contains(videoId)
}
fun containsBefore(videoId: String): Boolean {
return queue.contains(videoId) && queue.indexOf(videoId) < queue.indexOf(currentVideoId)
}
fun updateCurrent(videoId: String) {
currentVideoId = videoId
if (!contains(videoId)) add(videoId)
}
}