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 var SELECTED_PLAYLIST_ID: String? = null
// history of played videos in the current lifecycle // 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) shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
} }
context?.getString(R.string.add_to_queue) -> { 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) super.onViewCreated(view, savedInstanceState)
context?.hideKeyboard(view) context?.hideKeyboard(view)
// clear the playing queue
Globals.playingQueue.clear()
setUserPrefs() setUserPrefs()
if (autoplayEnabled) playerBinding.autoplayIV.setImageResource(R.drawable.ic_toggle_on) if (autoplayEnabled) playerBinding.autoplayIV.setImageResource(R.drawable.ic_toggle_on)
@ -748,7 +751,7 @@ class PlayerFragment : BaseFragment() {
} }
} }
} }
Globals.videoIds += videoId!! Globals.playingQueue += videoId!!
} }
run() run()
} }
@ -1053,13 +1056,13 @@ class PlayerFragment : BaseFragment() {
// next and previous buttons // next and previous buttons
playerBinding.skipPrev.visibility = if ( playerBinding.skipPrev.visibility = if (
skipButtonsEnabled && Globals.videoIds.indexOf(videoId!!) != 0 skipButtonsEnabled && Globals.playingQueue.indexOf(videoId!!) != 0
) View.VISIBLE else View.INVISIBLE ) View.VISIBLE else View.INVISIBLE
playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE
playerBinding.skipPrev.setOnClickListener { playerBinding.skipPrev.setOnClickListener {
val index = Globals.videoIds.indexOf(videoId!!) - 1 val index = Globals.playingQueue.indexOf(videoId!!) - 1
videoId = Globals.videoIds[index] videoId = Globals.playingQueue[index]
playVideo() playVideo()
} }

View File

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

View File

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