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,44 +716,39 @@ 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!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response") Log.e(TAG, "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} }
runOnUiThread { runOnUiThread {
// set media sources for the player // set media sources for the player
setResolutionAndSubtitles(streams) setResolutionAndSubtitles(streams)
prepareExoPlayerView() prepareExoPlayerView()
initializePlayerView(streams) initializePlayerView(streams)
if (!isLive) seekToWatchPosition() if (!isLive) seekToWatchPosition()
exoPlayer.prepare() exoPlayer.prepare()
exoPlayer.play() exoPlayer.play()
exoPlayerView.useController = true exoPlayerView.useController = true
initializePlayerNotification() initializePlayerNotification()
if (sponsorBlockEnabled) fetchSponsorBlockSegments() if (sponsorBlockEnabled) fetchSponsorBlockSegments()
// show comments if related streams disabled // show comments if related streams disabled
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()
} }
/** /**
@ -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,17 +17,15 @@ class AutoPlayHelper(
currentVideoId: String, currentVideoId: String,
relatedStreams: List<StreamItem> relatedStreams: List<StreamItem>
): String? { ): String? {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) return if (Globals.playingQueue.last() != currentVideoId) {
return if (Globals.playingQueue.size > currentVideoIndex + 1) { val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
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? {
@ -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
}
} }