same for background mode

This commit is contained in:
Bnyro 2022-08-10 15:53:34 +02:00
parent ed87a7b33a
commit 103376eb6b
5 changed files with 24 additions and 16 deletions

View File

@ -18,5 +18,5 @@ object Globals {
var SELECTED_PLAYLIST_ID: String? = null
// history of played videos in the current lifecycle
val videoIds = mutableListOf<String>()
val playingQueue = mutableListOf<String>()
}

View File

@ -83,7 +83,7 @@ class VideoOptionsDialog(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.add_to_queue) -> {
Globals.videoIds += videoId
Globals.playingQueue += videoId
}
}
}

View File

@ -201,6 +201,9 @@ class PlayerFragment : BaseFragment() {
super.onViewCreated(view, savedInstanceState)
context?.hideKeyboard(view)
// clear the playing queue
Globals.playingQueue.clear()
setUserPrefs()
if (autoplayEnabled) playerBinding.autoplayIV.setImageResource(R.drawable.ic_toggle_on)
@ -748,7 +751,7 @@ class PlayerFragment : BaseFragment() {
}
}
}
Globals.videoIds += videoId!!
Globals.playingQueue += videoId!!
}
run()
}
@ -1053,13 +1056,13 @@ class PlayerFragment : BaseFragment() {
// next and previous buttons
playerBinding.skipPrev.visibility = if (
skipButtonsEnabled && Globals.videoIds.indexOf(videoId!!) != 0
skipButtonsEnabled && Globals.playingQueue.indexOf(videoId!!) != 0
) View.VISIBLE else View.INVISIBLE
playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE
playerBinding.skipPrev.setOnClickListener {
val index = Globals.videoIds.indexOf(videoId!!) - 1
videoId = Globals.videoIds[index]
val index = Globals.playingQueue.indexOf(videoId!!) - 1
videoId = Globals.playingQueue[index]
playVideo()
}

View File

@ -12,6 +12,7 @@ import android.os.Looper
import android.widget.Toast
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.BACKGROUND_CHANNEL_ID
import com.github.libretube.Globals
import com.github.libretube.PLAYER_NOTIFICATION_ID
import com.github.libretube.R
import com.github.libretube.obj.Segment
@ -78,7 +79,7 @@ class BackgroundMode : Service() {
/**
* The [videoId] of the next stream for autoplay
*/
private lateinit var nextStreamId: String
private var nextStreamId: String? = null
/**
* Helper for finding the next video in the playlist
@ -111,6 +112,9 @@ class BackgroundMode : Service() {
*/
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
// clear the playing queue
Globals.playingQueue.clear()
// get the intent arguments
videoId = intent?.getStringExtra("videoId")!!
playlistId = intent.getStringExtra("playlistId")
@ -135,6 +139,8 @@ class BackgroundMode : Service() {
videoId: String,
seekToPosition: Long = 0
) {
// append the video to the playing queue
Globals.playingQueue += videoId
runBlocking {
val job = launch {
streams = RetrofitInstance.api.getStreams(videoId)
@ -217,8 +223,7 @@ class BackgroundMode : Service() {
if (!this::autoPlayHelper.isInitialized) autoPlayHelper = AutoPlayHelper(playlistId!!)
// search for the next videoId in the playlist
CoroutineScope(Dispatchers.IO).launch {
val nextId = autoPlayHelper.getNextVideoId(videoId)
if (nextId != null) nextStreamId = nextId
nextStreamId = autoPlayHelper.getNextVideoId(videoId, streams!!.relatedStreams!!)
}
}
@ -226,10 +231,10 @@ class BackgroundMode : Service() {
* Plays the first related video to the current (used when the playback of the current video ended)
*/
private fun playNextVideo() {
if (!this::nextStreamId.isInitialized || nextStreamId == videoId) return
if (nextStreamId == null || nextStreamId == videoId) return
// play new video on background
this.videoId = nextStreamId
this.videoId = nextStreamId!!
this.segmentData = null
playAudio(videoId)
}

View File

@ -17,9 +17,9 @@ class AutoPlayHelper(
currentVideoId: String,
relatedStreams: List<StreamItem>
): String? {
val currentVideoIndex = Globals.videoIds.indexOf(currentVideoId)
return if (Globals.videoIds.size > currentVideoIndex + 1) {
Globals.videoIds[currentVideoIndex + 1]
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
return if (Globals.playingQueue.size > currentVideoIndex + 1) {
Globals.playingQueue[currentVideoIndex + 1]
} else if (playlistId == null) getNextTrendingVideoId(
currentVideoId,
relatedStreams
@ -36,8 +36,8 @@ class AutoPlayHelper(
var nextStreamId: String? = null
while (nextStreamId == null ||
(
Globals.videoIds.contains(nextStreamId) &&
Globals.videoIds.indexOf(videoId) > Globals.videoIds.indexOf(
Globals.playingQueue.contains(nextStreamId) &&
Globals.playingQueue.indexOf(videoId) > Globals.playingQueue.indexOf(
nextStreamId
)
)