From 552131bd2c2353a0a1e3bd607198d3381ed73c80 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 14 Jan 2023 11:53:13 +0100 Subject: [PATCH 1/2] Don't clear queue when entering audio mode --- .../java/com/github/libretube/ui/fragments/PlayerFragment.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 2e0e9d0ed..f041bd46f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -581,8 +581,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { saveWatchPosition() - // clear the playing queue and release the player - PlayingQueue.resetToDefaults() + // release the player nowPlayingNotification.destroySelfAndPlayer() activity?.requestedOrientation = From 773a41d8300d1364d17864127ec0af4ea3dee5af Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 14 Jan 2023 12:01:14 +0100 Subject: [PATCH 2/2] Keep the queue when entering the audio only mode --- .../libretube/services/BackgroundMode.kt | 40 +++++++++++-------- .../github/libretube/util/BackgroundHelper.kt | 6 ++- 2 files changed, 27 insertions(+), 19 deletions(-) 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 a2bb1b95a..a316f76eb 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -134,9 +134,10 @@ class BackgroundMode : Service() { videoId = intent?.getStringExtra(IntentData.videoId)!! playlistId = intent.getStringExtra(IntentData.playlistId) val position = intent.getLongExtra(IntentData.position, 0L) + val keepQueue = intent.getBooleanExtra(IntentData.keepQueue, false) // play the audio in the background - loadAudio(videoId, position) + loadAudio(videoId, position, keepQueue) PlayingQueue.setOnQueueTapListener { streamItem -> streamItem.url?.toID()?.let { playNextVideo(it) } @@ -168,7 +169,8 @@ class BackgroundMode : Service() { */ private fun loadAudio( videoId: String, - seekToPosition: Long = 0 + seekToPosition: Long = 0, + keepQueue: Boolean = false ) { CoroutineScope(Dispatchers.IO).launch { streams = runCatching { @@ -176,21 +178,9 @@ class BackgroundMode : Service() { }.getOrNull() ?: return@launch // add the playlist video to the queue - if (PlayingQueue.isEmpty() && playlistId != null) { - streams?.toStreamItem(videoId)?.let { - PlayingQueue.insertPlaylist(playlistId!!, it) - } - } else if (PlayingQueue.isEmpty() && channelId != null) { - streams?.toStreamItem(videoId)?.let { - PlayingQueue.insertChannel(channelId!!, it) - } - } else { - streams?.toStreamItem(videoId)?.let { - PlayingQueue.updateCurrent(it) - } - streams?.relatedStreams?.toTypedArray()?.let { - if (PlayerHelper.autoInsertRelatedVideos) PlayingQueue.add(*it) - } + if (PlayingQueue.isEmpty() && !keepQueue) updateQueue() + streams?.toStreamItem(videoId)?.let { + PlayingQueue.updateCurrent(it) } handler.post { @@ -371,6 +361,22 @@ class BackgroundMode : Service() { } } + private fun updateQueue() { + if (playlistId != null) { + streams?.toStreamItem(videoId)?.let { + PlayingQueue.insertPlaylist(playlistId!!, it) + } + } else if (channelId != null) { + streams?.toStreamItem(videoId)?.let { + PlayingQueue.insertChannel(channelId!!, it) + } + } else { + streams?.relatedStreams?.toTypedArray()?.let { + if (PlayerHelper.autoInsertRelatedVideos) PlayingQueue.add(*it) + } + } + } + /** * Stop the service when app is removed from the task manager. */ diff --git a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt index 2b5f942c9..5c54f8264 100644 --- a/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackgroundHelper.kt @@ -21,14 +21,16 @@ object BackgroundHelper { videoId: String, position: Long? = null, playlistId: String? = null, - channelId: String? = null + channelId: String? = null, + keepQueue: Boolean? = null ) { // create an intent for the background mode service val intent = Intent(context, BackgroundMode::class.java) intent.putExtra(IntentData.videoId, videoId) intent.putExtra(IntentData.playlistId, playlistId) intent.putExtra(IntentData.channelId, channelId) - intent.putExtra("position", position) + intent.putExtra(IntentData.position, position) + intent.putExtra(IntentData.keepQueue, keepQueue) // start the background mode as foreground service if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {