From ffb3dd6743e0d262ec4ebef39886cfe2c07177f9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 10 Aug 2022 16:23:57 +0200 Subject: [PATCH] final touches --- .../libretube/fragments/PlayerFragment.kt | 69 +++++++++---------- .../libretube/services/BackgroundMode.kt | 16 +++-- .../github/libretube/util/AutoPlayHelper.kt | 21 ++++-- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 068706132..f8413534b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -716,44 +716,39 @@ class PlayerFragment : BaseFragment() { } private fun playVideo() { - fun run() { - lifecycleScope.launchWhenCreated { - streams = try { - RetrofitInstance.api.getStreams(videoId!!) - } catch (e: IOException) { - println(e) - Log.e(TAG, "IOException, you might not have internet connection") - Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() - return@launchWhenCreated - } catch (e: HttpException) { - Log.e(TAG, "HttpException, unexpected response") - Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() - return@launchWhenCreated - } - - runOnUiThread { - // set media sources for the player - setResolutionAndSubtitles(streams) - prepareExoPlayerView() - initializePlayerView(streams) - if (!isLive) seekToWatchPosition() - exoPlayer.prepare() - exoPlayer.play() - exoPlayerView.useController = true - initializePlayerNotification() - if (sponsorBlockEnabled) fetchSponsorBlockSegments() - // show comments if related streams disabled - if (!relatedStreamsEnabled) toggleComments() - // prepare for autoplay - if (autoplayEnabled) setNextStream() - if (watchHistoryEnabled) { - PreferenceHelper.addToWatchHistory(videoId!!, streams) - } - } + Globals.playingQueue += videoId!! + lifecycleScope.launchWhenCreated { + streams = try { + RetrofitInstance.api.getStreams(videoId!!) + } catch (e: IOException) { + println(e) + Log.e(TAG, "IOException, you might not have internet connection") + Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() + return@launchWhenCreated + } catch (e: HttpException) { + Log.e(TAG, "HttpException, unexpected response") + Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() + return@launchWhenCreated + } + + runOnUiThread { + // set media sources for the player + setResolutionAndSubtitles(streams) + prepareExoPlayerView() + initializePlayerView(streams) + if (!isLive) seekToWatchPosition() + exoPlayer.prepare() + exoPlayer.play() + exoPlayerView.useController = true + initializePlayerNotification() + if (sponsorBlockEnabled) fetchSponsorBlockSegments() + // show comments if related streams disabled + if (!relatedStreamsEnabled) toggleComments() + // prepare for autoplay + if (autoplayEnabled) setNextStream() + if (watchHistoryEnabled) PreferenceHelper.addToWatchHistory(videoId!!, streams) } - Globals.playingQueue += videoId!! } - run() } /** @@ -828,6 +823,8 @@ class PlayerFragment : BaseFragment() { private fun playNextVideo() { if (nextStreamId == null) return // 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 saveWatchPosition() // forces the comments to reload for the new video 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 0c2fd9bb0..0ad0f53b8 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -30,7 +30,6 @@ import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.audio.AudioAttributes -import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -87,7 +86,12 @@ class BackgroundMode : Service() { 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() { super.onCreate() @@ -174,7 +178,7 @@ class BackgroundMode : Service() { fetchSponsorBlockSegments() - setNextStream() + if (autoplay) setNextStream() } } @@ -200,7 +204,6 @@ class BackgroundMode : Service() { override fun onPlaybackStateChanged(@Player.State state: Int) { when (state) { Player.STATE_ENDED -> { - val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true) if (autoplay) playNextVideo() } Player.STATE_IDLE -> { @@ -232,6 +235,8 @@ class BackgroundMode : Service() { */ private fun playNextVideo() { if (nextStreamId == null || nextStreamId == videoId) return + val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId) + if (nextQueueVideo != null) nextStreamId = nextQueueVideo // play new video on background this.videoId = nextStreamId!! @@ -240,8 +245,7 @@ class BackgroundMode : Service() { } /** - * Sets the [MediaItem] with the [streams] into the [player]. Also creates a [MediaSessionConnector] - * with the [mediaSession] and attach it to the [player]. + * Sets the [MediaItem] with the [streams] into the [player] */ private fun setMediaItem() { streams?.let { diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index 043af6f72..d747681ea 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -17,17 +17,15 @@ class AutoPlayHelper( currentVideoId: String, relatedStreams: List ): String? { - val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) - return if (Globals.playingQueue.size > currentVideoIndex + 1) { + return if (Globals.playingQueue.last() != currentVideoId) { + val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) Globals.playingQueue[currentVideoIndex + 1] } else if (playlistId == null) getNextTrendingVideoId( currentVideoId, relatedStreams - ) else { - getNextPlaylistVideoId( - currentVideoId - ) - } + ) else getNextPlaylistVideoId( + currentVideoId + ) } private fun getNextTrendingVideoId(videoId: String, relatedStreams: List): String? { @@ -77,4 +75,13 @@ class AutoPlayHelper( // return null when no nextPage is found 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 + } }