Merge pull request #1776 from Bnyro/master

Play video when clicking queue items
This commit is contained in:
Bnyro 2022-11-06 11:53:10 +01:00 committed by GitHub
commit cae2f237ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 7 deletions

View File

@ -24,6 +24,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper
@ -124,6 +125,10 @@ class BackgroundMode : Service() {
// play the audio in the background
loadAudio(videoId, position)
PlayingQueue.setOnQueueTapListener { streamItem ->
streamItem.url?.toID()?.let { playNextVideo(it) }
}
updateWatchPosition()
} catch (e: Exception) {
onDestroy()
@ -264,8 +269,8 @@ class BackgroundMode : Service() {
/**
* Plays the first related video to the current (used when the playback of the current video ended)
*/
private fun playNextVideo() {
val nextVideo = PlayingQueue.getNext()
private fun playNextVideo(nextId: String? = null) {
val nextVideo = nextId ?: PlayingQueue.getNext()
// play new video on background
if (nextVideo != null) {

View File

@ -13,7 +13,6 @@ import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.ThemeHelper
class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
private val currentIndex = PlayingQueue.currentIndex()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayingQueueViewHolder {
val binding = QueueRowBinding.inflate(
@ -37,6 +36,7 @@ class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
videoInfo.text = streamItem.uploaderName + "" +
DateUtils.formatElapsedTime(streamItem.duration ?: 0)
val currentIndex = PlayingQueue.currentIndex()
root.setBackgroundColor(
if (currentIndex == position) {
ThemeHelper.getThemeColor(root.context, android.R.attr.colorControlHighlight)
@ -44,6 +44,13 @@ class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
Color.TRANSPARENT
}
)
root.setOnClickListener {
val oldIndex = PlayingQueue.currentIndex()
PlayingQueue.onQueueItemSelected(position)
notifyItemChanged(oldIndex)
notifyItemChanged(position)
}
}
}
}

View File

@ -503,6 +503,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
try {
// clear the playing queue
PlayingQueue.clear()
PlayingQueue.removeOnQueueTapListener()
saveWatchPosition()
nowPlayingNotification.destroySelfAndPlayer()
@ -602,6 +603,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
PlayingQueue.updateCurrent(streams.toStreamItem(videoId!!))
}
PlayingQueue.setOnQueueTapListener { streamItem ->
streamItem.url?.toID()?.let { playNextVideo(it) }
}
runOnUiThread {
// hide the button to skip SponsorBlock segments manually
binding.sbSkipBtn.visibility = View.GONE
@ -702,8 +707,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
// used for autoplay and skipping to next video
private fun playNextVideo() {
val nextVideoId = PlayingQueue.getNext()
private fun playNextVideo(nextId: String? = null) {
val nextVideoId = nextId ?: PlayingQueue.getNext()
// by making sure that the next and the current video aren't the same
saveWatchPosition()
@ -987,7 +992,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer)
// enable the chapters dialog in the player
val titles = chapters.map { "${it.title} (${it.start?.let { DateUtils.formatElapsedTime(it) }})" }
val titles = chapters.map { chapter ->
"${chapter.title} (${chapter.start?.let { DateUtils.formatElapsedTime(it) }})"
}
playerBinding.chapterLL.setOnClickListener {
if (viewModel.isFullscreen.value!!) {
MaterialAlertDialogBuilder(requireContext())

View File

@ -1,5 +1,6 @@
package com.github.libretube.util
import android.util.Log
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move
@ -11,6 +12,7 @@ import kotlinx.coroutines.launch
object PlayingQueue {
private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null
private var onQueueTapListener: (StreamItem) -> Unit = {}
fun add(vararg streamItem: StreamItem) {
streamItem.forEach {
@ -116,4 +118,22 @@ object PlayingQueue {
}
}
}
fun onQueueItemSelected(index: Int) {
try {
val streamItem = queue[index]
updateCurrent(streamItem)
onQueueTapListener.invoke(streamItem)
} catch (e: Exception) {
Log.e("Queue on tap", "lifecycle already ended")
}
}
fun setOnQueueTapListener(listener: (StreamItem) -> Unit) {
onQueueTapListener = listener
}
fun removeOnQueueTapListener() {
onQueueTapListener = {}
}
}

View File

@ -51,7 +51,6 @@
android:layout_gravity="center"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:rotation="180"
android:src="@drawable/ic_arrow_down" />
<ImageView