final touches

This commit is contained in:
Bnyro 2022-08-10 16:23:57 +02:00
parent 103376eb6b
commit ffb3dd6743
3 changed files with 57 additions and 49 deletions

View File

@ -716,7 +716,7 @@ class PlayerFragment : BaseFragment() {
} }
private fun playVideo() { private fun playVideo() {
fun run() { Globals.playingQueue += videoId!!
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
streams = try { streams = try {
RetrofitInstance.api.getStreams(videoId!!) RetrofitInstance.api.getStreams(videoId!!)
@ -746,15 +746,10 @@ class PlayerFragment : BaseFragment() {
if (!relatedStreamsEnabled) toggleComments() if (!relatedStreamsEnabled) toggleComments()
// prepare for autoplay // prepare for autoplay
if (autoplayEnabled) setNextStream() if (autoplayEnabled) setNextStream()
if (watchHistoryEnabled) { if (watchHistoryEnabled) PreferenceHelper.addToWatchHistory(videoId!!, streams)
PreferenceHelper.addToWatchHistory(videoId!!, streams)
} }
} }
} }
Globals.playingQueue += videoId!!
}
run()
}
/** /**
* set the videoId of the next stream for autoplay * set the videoId of the next stream for autoplay
@ -828,6 +823,8 @@ class PlayerFragment : BaseFragment() {
private fun playNextVideo() { private fun playNextVideo() {
if (nextStreamId == null) return if (nextStreamId == null) return
// check whether there is a new video in the queue // check whether there is a new video in the queue
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId!!)
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
// by making sure that the next and the current video aren't the same // by making sure that the next and the current video aren't the same
saveWatchPosition() saveWatchPosition()
// forces the comments to reload for the new video // forces the comments to reload for the new video

View File

@ -30,7 +30,6 @@ import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -87,7 +86,12 @@ class BackgroundMode : Service() {
private lateinit var autoPlayHelper: AutoPlayHelper private lateinit var autoPlayHelper: AutoPlayHelper
/** /**
* Setting the required [notification] for running as a foreground service * Autoplay Preference
*/
private val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
/**
* Setting the required [Notification] for running as a foreground service
*/ */
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -174,7 +178,7 @@ class BackgroundMode : Service() {
fetchSponsorBlockSegments() fetchSponsorBlockSegments()
setNextStream() if (autoplay) setNextStream()
} }
} }
@ -200,7 +204,6 @@ class BackgroundMode : Service() {
override fun onPlaybackStateChanged(@Player.State state: Int) { override fun onPlaybackStateChanged(@Player.State state: Int) {
when (state) { when (state) {
Player.STATE_ENDED -> { Player.STATE_ENDED -> {
val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
if (autoplay) playNextVideo() if (autoplay) playNextVideo()
} }
Player.STATE_IDLE -> { Player.STATE_IDLE -> {
@ -232,6 +235,8 @@ class BackgroundMode : Service() {
*/ */
private fun playNextVideo() { private fun playNextVideo() {
if (nextStreamId == null || nextStreamId == videoId) return if (nextStreamId == null || nextStreamId == videoId) return
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId)
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
// play new video on background // play new video on background
this.videoId = nextStreamId!! this.videoId = nextStreamId!!
@ -240,8 +245,7 @@ class BackgroundMode : Service() {
} }
/** /**
* Sets the [MediaItem] with the [streams] into the [player]. Also creates a [MediaSessionConnector] * Sets the [MediaItem] with the [streams] into the [player]
* with the [mediaSession] and attach it to the [player].
*/ */
private fun setMediaItem() { private fun setMediaItem() {
streams?.let { streams?.let {

View File

@ -17,18 +17,16 @@ class AutoPlayHelper(
currentVideoId: String, currentVideoId: String,
relatedStreams: List<StreamItem> relatedStreams: List<StreamItem>
): String? { ): String? {
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
return if (Globals.playingQueue.size > currentVideoIndex + 1) {
Globals.playingQueue[currentVideoIndex + 1] Globals.playingQueue[currentVideoIndex + 1]
} else if (playlistId == null) getNextTrendingVideoId( } else if (playlistId == null) getNextTrendingVideoId(
currentVideoId, currentVideoId,
relatedStreams relatedStreams
) else { ) else getNextPlaylistVideoId(
getNextPlaylistVideoId(
currentVideoId currentVideoId
) )
} }
}
private fun getNextTrendingVideoId(videoId: String, relatedStreams: List<StreamItem>): String? { private fun getNextTrendingVideoId(videoId: String, relatedStreams: List<StreamItem>): String? {
// don't play a video if it got played before already // don't play a video if it got played before already
@ -77,4 +75,13 @@ class AutoPlayHelper(
// return null when no nextPage is found // return null when no nextPage is found
return null return null
} }
fun getNextPlayingQueueVideoId(
currentVideoId: String
): String? {
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
Globals.playingQueue[currentVideoIndex + 1]
} else null
}
} }