diff --git a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt index 298b4c490..566d64774 100644 --- a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt @@ -1,7 +1,6 @@ package com.github.libretube.dialogs import android.app.Dialog -import android.content.Context import android.os.Bundle import android.util.Log import android.widget.ArrayAdapter @@ -10,11 +9,14 @@ import androidx.fragment.app.DialogFragment import com.github.libretube.R import com.github.libretube.obj.PlaylistId import com.github.libretube.preferences.PreferenceHelper +import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.RetrofitInstance +import com.github.libretube.util.toID import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import retrofit2.HttpException import java.io.IOException @@ -27,6 +29,7 @@ class PlaylistOptionsDialog( override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { // options for the dialog var optionsList = listOf( + context?.getString(R.string.playOnBackground), context?.getString(R.string.clonePlaylist), context?.getString(R.string.share) ) @@ -49,6 +52,18 @@ class PlaylistOptionsDialog( ) ) { _, which -> when (optionsList[which]) { + // play the playlist in the background + context?.getString(R.string.playOnBackground) -> { + runBlocking { + val playlist = if (isOwner) RetrofitInstance.authApi.getPlaylist(playlistId) + else RetrofitInstance.api.getPlaylist(playlistId) + BackgroundHelper.playOnBackground( + context = requireContext(), + videoId = playlist.relatedStreams!![0].url.toID(), + playlistId = playlistId + ) + } + } // Clone the playlist to the users Piped account context?.getString(R.string.clonePlaylist) -> { val token = PreferenceHelper.getToken() 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 c2d281630..0a70a285e 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -112,11 +112,11 @@ class BackgroundMode : Service() { try { // get the intent arguments videoId = intent?.getStringExtra("videoId")!! - playlistId = intent.getStringExtra(playlistId) + playlistId = intent.getStringExtra("playlistId") val position = intent.getLongExtra("position", 0L) // initialize the playlist autoPlay Helper - autoPlayHelper = AutoPlayHelper(playlistId!!) + if (playlistId != null) autoPlayHelper = AutoPlayHelper(playlistId!!) // play the audio in the background playAudio(videoId, position) @@ -159,6 +159,8 @@ class BackgroundMode : Service() { if (seekToPosition != 0L) player?.seekTo(seekToPosition) fetchSponsorBlockSegments() + + setNextStream() } } @@ -216,7 +218,7 @@ class BackgroundMode : Service() { * Plays the first related video to the current (used when the playback of the current video ended) */ private fun playNextVideo() { - if (nextStreamId == videoId || !this::nextStreamId.isInitialized) return + if (!this::nextStreamId.isInitialized || nextStreamId == videoId) return // play new video on background this.videoId = nextStreamId