diff --git a/app/src/main/java/com/github/libretube/enums/PlayerCommand.kt b/app/src/main/java/com/github/libretube/enums/PlayerCommand.kt index 4fc8de83a..04045581d 100644 --- a/app/src/main/java/com/github/libretube/enums/PlayerCommand.kt +++ b/app/src/main/java/com/github/libretube/enums/PlayerCommand.kt @@ -8,5 +8,6 @@ enum class PlayerCommand { SET_AUDIO_LANGUAGE, SET_SUBTITLE, SET_SB_AUTO_SKIP_ENABLED, - PLAY_VIDEO_BY_ID + PLAY_VIDEO_BY_ID, + SET_AUTOPLAY_COUNTDOWN_ENABLED } \ No newline at end of file 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 5aacd5aea..e18c7642a 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -61,14 +61,15 @@ open class OnlinePlayerService : AbstractPlayerService() { /** * The response that gets when called the Api. */ - var streams: Streams? = null - private set + private var streams: Streams? = null // SponsorBlock Segment data private var sponsorBlockAutoSkip = true private var sponsorBlockSegments = listOf() private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories() + private var autoPlayCountdownEnabled = false + private val scope = CoroutineScope(Dispatchers.IO) private val playerListener = object : Player.Listener { @@ -185,8 +186,7 @@ open class OnlinePlayerService : AbstractPlayerService() { return } - if (!PlayerHelper.isAutoPlayEnabled(playlistId != null)) return - if (!isAudioOnlyPlayer && PlayerHelper.autoPlayCountdown) return + if (!PlayerHelper.isAutoPlayEnabled(playlistId != null) || autoPlayCountdownEnabled) return } val nextVideo = nextId ?: PlayingQueue.getNext() ?: return @@ -243,6 +243,8 @@ open class OnlinePlayerService : AbstractPlayerService() { if (args.containsKey(PlayerCommand.SET_SB_AUTO_SKIP_ENABLED.name)) { sponsorBlockAutoSkip = args.getBoolean(PlayerCommand.SET_SB_AUTO_SKIP_ENABLED.name) + } else if (args.containsKey(PlayerCommand.SET_AUTOPLAY_COUNTDOWN_ENABLED.name)) { + autoPlayCountdownEnabled = args.getBoolean(PlayerCommand.SET_AUTOPLAY_COUNTDOWN_ENABLED.name) } } 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 bf04d7c14..cc250c909 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 @@ -164,6 +164,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // True when the video was closed through the close button on PiP mode private var closedVideo = false + private var autoPlayCountdownEnabled = PlayerHelper.autoPlayCountdown + /** * The orientation of the `fragment_player.xml` that's currently used * This is needed in order to figure out if the current layout is the landscape one or not. @@ -269,7 +271,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 (PlayerHelper.isAutoPlayEnabled(playlistId != null) && PlayerHelper.autoPlayCountdown) { + if (PlayerHelper.isAutoPlayEnabled(playlistId != null) && autoPlayCountdownEnabled) { showAutoPlayCountdown() } else { binding.player.showControllerPermanently() @@ -459,7 +461,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { bundleOf(IntentData.playerData to playerData) ) { if (_binding == null) { - playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY) + playerController.sendCustomCommand( + AbstractPlayerService.stopServiceCommand, + Bundle.EMPTY + ) playerController.release() return@startMediaService } @@ -816,11 +821,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // disable video stream since it's not needed when screen off if (!isInteractive) { - playerController.sendCustomCommand( - AbstractPlayerService.runPlayerActionCommand, bundleOf( - PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name to true - ) - ) + // disable the autoplay countdown while the screen is off + setAutoPlayCountdownEnabled(false) + + // disable loading the video track while screen is off + setVideoTrackTypeDisabled(true) } // pause player if screen off and setting enabled @@ -838,14 +843,33 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { closedVideo = false } + // re-enable the autoplay countdown + setAutoPlayCountdownEnabled(PlayerHelper.autoPlayCountdown) + // re-enable and load video stream - if (::playerController.isInitialized) { - playerController.sendCustomCommand( - AbstractPlayerService.runPlayerActionCommand, bundleOf( - PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name to false - ) + setVideoTrackTypeDisabled(false) + } + + private fun setAutoPlayCountdownEnabled(enabled: Boolean) { + if (!::playerController.isInitialized) return + + this.autoPlayCountdownEnabled = enabled + + playerController.sendCustomCommand( + AbstractPlayerService.runPlayerActionCommand, bundleOf( + PlayerCommand.SET_AUTOPLAY_COUNTDOWN_ENABLED.name to enabled ) - } + ) + } + + private fun setVideoTrackTypeDisabled(disabled: Boolean) { + if (!::playerController.isInitialized) return + + playerController.sendCustomCommand( + AbstractPlayerService.runPlayerActionCommand, bundleOf( + PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name to disabled + ) + ) } override fun onDestroy() { @@ -857,7 +881,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playerController.removeListener(playerListener) playerController.pause() - playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY) + playerController.sendCustomCommand( + AbstractPlayerService.stopServiceCommand, + Bundle.EMPTY + ) playerController.release() } @@ -958,6 +985,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { ) ) + setAutoPlayCountdownEnabled(PlayerHelper.autoPlayCountdown) + // set the default subtitle if available updateCurrentSubtitle(viewModel.currentSubtitle)