From 103376eb6bfe957ab05491794df94df888862f59 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 10 Aug 2022 15:53:34 +0200 Subject: [PATCH] same for background mode --- app/src/main/java/com/github/libretube/Globals.kt | 2 +- .../libretube/dialogs/VideoOptionsDialog.kt | 2 +- .../github/libretube/fragments/PlayerFragment.kt | 11 +++++++---- .../github/libretube/services/BackgroundMode.kt | 15 ++++++++++----- .../com/github/libretube/util/AutoPlayHelper.kt | 10 +++++----- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/Globals.kt b/app/src/main/java/com/github/libretube/Globals.kt index bfb98c573..5db9eabea 100644 --- a/app/src/main/java/com/github/libretube/Globals.kt +++ b/app/src/main/java/com/github/libretube/Globals.kt @@ -18,5 +18,5 @@ object Globals { var SELECTED_PLAYLIST_ID: String? = null // history of played videos in the current lifecycle - val videoIds = mutableListOf() + val playingQueue = mutableListOf() } diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index e86b2984c..f10d1d254 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -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 } } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 27c703581..068706132 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -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() } diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index a4c37e96d..0c2fd9bb0 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -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) } diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index e7072c4d6..043af6f72 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -17,9 +17,9 @@ class AutoPlayHelper( currentVideoId: String, relatedStreams: List ): 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 ) )