diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index c45361638..6a628338f 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -45,7 +45,6 @@ import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.togglePlayPauseState import com.github.libretube.extensions.updateParameters import com.github.libretube.obj.VideoStats -import com.github.libretube.util.PlayingQueue import com.github.libretube.util.TextUtils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -357,18 +356,9 @@ object PlayerHelper { true ) - fun shouldPlayNextVideo(isPlaylist: Boolean = false): Boolean { - // if there is no next video, it obviously should not be played - if (!PlayingQueue.hasNext()) { - return false - } - - return autoPlayEnabled || ( - isPlaylist && PreferenceHelper.getBoolean( - PreferenceKeys.AUTOPLAY_PLAYLISTS, - false - ) - ) + fun isAutoPlayEnabled(isPlaylist: Boolean = false): Boolean { + return autoPlayEnabled || (isPlaylist && PreferenceHelper + .getBoolean(PreferenceKeys.AUTOPLAY_PLAYLISTS, false)) } private val handleAudioFocus diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index 5e05280b5..6412cc27f 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -131,7 +131,7 @@ class OnlinePlayerService : LifecycleService() { when (state) { Player.STATE_ENDED -> { - if (PlayerHelper.shouldPlayNextVideo(playlistId != null) && !isTransitioning) playNextVideo() + if (!isTransitioning) playNextVideo() } Player.STATE_IDLE -> { @@ -353,6 +353,10 @@ class OnlinePlayerService : LifecycleService() { return } + saveWatchPosition() + + if (!PlayerHelper.isAutoPlayEnabled(playlistId != null)) return + val nextVideo = nextId ?: PlayingQueue.getNext() ?: return // play new video on background diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index bc48954d6..249acfe78 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -294,7 +294,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // check if video has ended, next video is available and autoplay is enabled/the video is part of a played playlist. if (playbackState == Player.STATE_ENDED) { - if (!isTransitioning && PlayerHelper.shouldPlayNextVideo(playlistId != null)) { + if (!isTransitioning) { isTransitioning = true if (PlayerHelper.autoPlayCountdown) { showAutoPlayCountdown() @@ -995,13 +995,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { return } + if (!PlayerHelper.isAutoPlayEnabled(playlistId != null)) return + // save the current watch position before starting the next video saveWatchPosition() - val nextVideoId = nextId ?: PlayingQueue.getNext() ?: return - + videoId = nextId ?: PlayingQueue.getNext() ?: return isTransitioning = true - videoId = nextVideoId // fix: if the fragment is recreated, play the current video, and not the initial one arguments?.run {