feat: disable autoplay countdown while screen off

This commit is contained in:
Bnyro 2024-11-20 11:34:31 +01:00
parent b7c0d984fe
commit af2c84f29b
3 changed files with 51 additions and 19 deletions

View File

@ -8,5 +8,6 @@ enum class PlayerCommand {
SET_AUDIO_LANGUAGE, SET_AUDIO_LANGUAGE,
SET_SUBTITLE, SET_SUBTITLE,
SET_SB_AUTO_SKIP_ENABLED, SET_SB_AUTO_SKIP_ENABLED,
PLAY_VIDEO_BY_ID PLAY_VIDEO_BY_ID,
SET_AUTOPLAY_COUNTDOWN_ENABLED
} }

View File

@ -61,14 +61,15 @@ open class OnlinePlayerService : AbstractPlayerService() {
/** /**
* The response that gets when called the Api. * The response that gets when called the Api.
*/ */
var streams: Streams? = null private var streams: Streams? = null
private set
// SponsorBlock Segment data // SponsorBlock Segment data
private var sponsorBlockAutoSkip = true private var sponsorBlockAutoSkip = true
private var sponsorBlockSegments = listOf<Segment>() private var sponsorBlockSegments = listOf<Segment>()
private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories() private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories()
private var autoPlayCountdownEnabled = false
private val scope = CoroutineScope(Dispatchers.IO) private val scope = CoroutineScope(Dispatchers.IO)
private val playerListener = object : Player.Listener { private val playerListener = object : Player.Listener {
@ -185,8 +186,7 @@ open class OnlinePlayerService : AbstractPlayerService() {
return return
} }
if (!PlayerHelper.isAutoPlayEnabled(playlistId != null)) return if (!PlayerHelper.isAutoPlayEnabled(playlistId != null) || autoPlayCountdownEnabled) return
if (!isAudioOnlyPlayer && PlayerHelper.autoPlayCountdown) return
} }
val nextVideo = nextId ?: PlayingQueue.getNext() ?: 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)) { if (args.containsKey(PlayerCommand.SET_SB_AUTO_SKIP_ENABLED.name)) {
sponsorBlockAutoSkip = args.getBoolean(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)
} }
} }

View File

@ -164,6 +164,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// True when the video was closed through the close button on PiP mode // True when the video was closed through the close button on PiP mode
private var closedVideo = false private var closedVideo = false
private var autoPlayCountdownEnabled = PlayerHelper.autoPlayCountdown
/** /**
* The orientation of the `fragment_player.xml` that's currently used * 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. * 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. // 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 (playbackState == Player.STATE_ENDED) {
if (PlayerHelper.isAutoPlayEnabled(playlistId != null) && PlayerHelper.autoPlayCountdown) { if (PlayerHelper.isAutoPlayEnabled(playlistId != null) && autoPlayCountdownEnabled) {
showAutoPlayCountdown() showAutoPlayCountdown()
} else { } else {
binding.player.showControllerPermanently() binding.player.showControllerPermanently()
@ -459,7 +461,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
bundleOf(IntentData.playerData to playerData) bundleOf(IntentData.playerData to playerData)
) { ) {
if (_binding == null) { if (_binding == null) {
playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY) playerController.sendCustomCommand(
AbstractPlayerService.stopServiceCommand,
Bundle.EMPTY
)
playerController.release() playerController.release()
return@startMediaService return@startMediaService
} }
@ -816,11 +821,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// disable video stream since it's not needed when screen off // disable video stream since it's not needed when screen off
if (!isInteractive) { if (!isInteractive) {
playerController.sendCustomCommand( // disable the autoplay countdown while the screen is off
AbstractPlayerService.runPlayerActionCommand, bundleOf( setAutoPlayCountdownEnabled(false)
PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name to true
) // disable loading the video track while screen is off
) setVideoTrackTypeDisabled(true)
} }
// pause player if screen off and setting enabled // pause player if screen off and setting enabled
@ -838,14 +843,33 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
closedVideo = false closedVideo = false
} }
// re-enable the autoplay countdown
setAutoPlayCountdownEnabled(PlayerHelper.autoPlayCountdown)
// re-enable and load video stream // re-enable and load video stream
if (::playerController.isInitialized) { setVideoTrackTypeDisabled(false)
playerController.sendCustomCommand( }
AbstractPlayerService.runPlayerActionCommand, bundleOf(
PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name to 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() { override fun onDestroy() {
@ -857,7 +881,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
playerController.removeListener(playerListener) playerController.removeListener(playerListener)
playerController.pause() playerController.pause()
playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY) playerController.sendCustomCommand(
AbstractPlayerService.stopServiceCommand,
Bundle.EMPTY
)
playerController.release() playerController.release()
} }
@ -958,6 +985,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
) )
) )
setAutoPlayCountdownEnabled(PlayerHelper.autoPlayCountdown)
// set the default subtitle if available // set the default subtitle if available
updateCurrentSubtitle(viewModel.currentSubtitle) updateCurrentSubtitle(viewModel.currentSubtitle)