playlist autoplay in background mode

This commit is contained in:
Bnyro 2022-08-08 11:08:44 +02:00
parent 42b34b44d2
commit 212152cef2
2 changed files with 21 additions and 4 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.dialogs package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
@ -10,11 +9,14 @@ import androidx.fragment.app.DialogFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -27,6 +29,7 @@ class PlaylistOptionsDialog(
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// options for the dialog // options for the dialog
var optionsList = listOf( var optionsList = listOf(
context?.getString(R.string.playOnBackground),
context?.getString(R.string.clonePlaylist), context?.getString(R.string.clonePlaylist),
context?.getString(R.string.share) context?.getString(R.string.share)
) )
@ -49,6 +52,18 @@ class PlaylistOptionsDialog(
) )
) { _, which -> ) { _, which ->
when (optionsList[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 // Clone the playlist to the users Piped account
context?.getString(R.string.clonePlaylist) -> { context?.getString(R.string.clonePlaylist) -> {
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()

View File

@ -112,11 +112,11 @@ class BackgroundMode : Service() {
try { try {
// get the intent arguments // get the intent arguments
videoId = intent?.getStringExtra("videoId")!! videoId = intent?.getStringExtra("videoId")!!
playlistId = intent.getStringExtra(playlistId) playlistId = intent.getStringExtra("playlistId")
val position = intent.getLongExtra("position", 0L) val position = intent.getLongExtra("position", 0L)
// initialize the playlist autoPlay Helper // initialize the playlist autoPlay Helper
autoPlayHelper = AutoPlayHelper(playlistId!!) if (playlistId != null) autoPlayHelper = AutoPlayHelper(playlistId!!)
// play the audio in the background // play the audio in the background
playAudio(videoId, position) playAudio(videoId, position)
@ -159,6 +159,8 @@ class BackgroundMode : Service() {
if (seekToPosition != 0L) player?.seekTo(seekToPosition) if (seekToPosition != 0L) player?.seekTo(seekToPosition)
fetchSponsorBlockSegments() 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) * Plays the first related video to the current (used when the playback of the current video ended)
*/ */
private fun playNextVideo() { private fun playNextVideo() {
if (nextStreamId == videoId || !this::nextStreamId.isInitialized) return if (!this::nextStreamId.isInitialized || nextStreamId == videoId) return
// play new video on background // play new video on background
this.videoId = nextStreamId this.videoId = nextStreamId