Hide captions in PiP and mini-player

This commit is contained in:
Bnyro 2022-12-22 11:17:34 +01:00
parent d81bbf1094
commit a2296802f0
3 changed files with 35 additions and 47 deletions

View File

@ -0,0 +1,6 @@
package com.github.libretube.extensions
import com.google.android.exoplayer2.trackselection.TrackSelector
fun TrackSelector.updateParameters() {
}

View File

@ -144,6 +144,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
*/ */
private lateinit var exoPlayer: ExoPlayer private lateinit var exoPlayer: ExoPlayer
private lateinit var trackSelector: DefaultTrackSelector private lateinit var trackSelector: DefaultTrackSelector
private var captionLanguage: String? = PlayerHelper.defaultSubtitleCode
/** /**
* Chapters and comments * Chapters and comments
@ -226,14 +227,14 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private fun initializeTransitionLayout() { private fun initializeTransitionLayout() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.binding.container.visibility = View.VISIBLE mainActivity.binding.container.visibility = View.VISIBLE
val mainMotionLayout = mainActivity.binding.mainMotionLayout
binding.playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener { binding.playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionStarted( override fun onTransitionStarted(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
startId: Int, startId: Int,
endId: Int endId: Int
) { ) {}
}
override fun onTransitionChange( override fun onTransitionChange(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
@ -241,8 +242,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
endId: Int, endId: Int,
progress: Float progress: Float
) { ) {
val mainMotionLayout =
mainActivity.binding.mainMotionLayout
mainMotionLayout.progress = abs(progress) mainMotionLayout.progress = abs(progress)
exoPlayerView.hideController() exoPlayerView.hideController()
exoPlayerView.useController = false exoPlayerView.useController = false
@ -251,16 +250,17 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) { override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
println(currentId)
val mainMotionLayout =
mainActivity.binding.mainMotionLayout
if (currentId == eId) { if (currentId == eId) {
viewModel.isMiniPlayerVisible.value = true viewModel.isMiniPlayerVisible.value = true
// disable captions
updateCaptionsLanguage(null)
exoPlayerView.useController = false exoPlayerView.useController = false
mainMotionLayout.progress = 1F mainMotionLayout.progress = 1F
(activity as MainActivity).requestOrientationChange() (activity as MainActivity).requestOrientationChange()
} else if (currentId == sId) { } else if (currentId == sId) {
viewModel.isMiniPlayerVisible.value = false viewModel.isMiniPlayerVisible.value = false
// re-enable captions
updateCaptionsLanguage(captionLanguage)
exoPlayerView.useController = true exoPlayerView.useController = true
mainMotionLayout.progress = 0F mainMotionLayout.progress = 0F
changeOrientationMode() changeOrientationMode()
@ -272,8 +272,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
triggerId: Int, triggerId: Int,
positive: Boolean, positive: Boolean,
progress: Float progress: Float
) { ) {}
}
}) })
binding.playerMotionLayout.progress = 1.toFloat() binding.playerMotionLayout.progress = 1.toFloat()
@ -1189,24 +1188,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
// set the default subtitle if available // set the default subtitle if available
val newParams = trackSelector.buildUponParameters() updateCaptionsLanguage(captionLanguage)
if (PlayerHelper.defaultSubtitleCode != "" && subtitleCodesList.contains(
PlayerHelper.defaultSubtitleCode
)
) {
newParams
.setPreferredTextLanguage(PlayerHelper.defaultSubtitleCode)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
} else {
newParams.setPreferredTextLanguage(null)
}
trackSelector.setParameters(newParams)
// set media source and resolution in the beginning // set media source and resolution in the beginning
setStreamSource( setStreamSource()
streams
)
} }
private fun setPlayerResolution(resolution: Int) { private fun setPlayerResolution(resolution: Int) {
@ -1215,7 +1200,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
trackSelector.setParameters(params) trackSelector.setParameters(params)
} }
private fun setStreamSource(streams: Streams) { private fun setStreamSource() {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "") val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "")
if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt()) if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt())
@ -1235,7 +1220,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
this.setMediaSource(uri, MimeTypes.APPLICATION_MPD) this.setMediaSource(uri, MimeTypes.APPLICATION_MPD)
} else if (streams.hls != null) { } else if (streams.hls != null) {
setMediaSource(streams.hls.toUri(), MimeTypes.APPLICATION_M3U8) setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8)
} else { } else {
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
} }
@ -1331,25 +1316,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems(subtitlesNamesList) { index -> .setSimpleItems(subtitlesNamesList) { index ->
val newParams = if (index != 0) { val language = if (index > 0) subtitleCodesList[index] else null
// caption selected updateCaptionsLanguage(language)
this.captionLanguage = language
// get the caption language code
val captionLanguageCode = subtitleCodesList[index]
// select the new caption preference
trackSelector.buildUponParameters()
.setPreferredTextLanguage(captionLanguageCode)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
} else {
// none selected
// disable captions
trackSelector.buildUponParameters()
.setPreferredTextLanguage(null)
}
// set the new caption language
trackSelector.setParameters(newParams)
} }
.show(childFragmentManager) .show(childFragmentManager)
} }
@ -1415,6 +1384,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.linLayout.visibility = View.GONE binding.linLayout.visibility = View.GONE
viewModel.isFullscreen.value = false viewModel.isFullscreen.value = false
updateCaptionsLanguage(null)
} else if (lifecycle.currentState == Lifecycle.State.CREATED) { } else if (lifecycle.currentState == Lifecycle.State.CREATED) {
// close button got clicked in PiP mode // close button got clicked in PiP mode
// destroying the fragment, player and notification // destroying the fragment, player and notification
@ -1430,9 +1401,18 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
binding.linLayout.visibility = View.VISIBLE binding.linLayout.visibility = View.VISIBLE
updateCaptionsLanguage(captionLanguage)
} }
} }
private fun updateCaptionsLanguage(language: String?) {
val params = trackSelector.buildUponParameters()
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
.setPreferredTextLanguage(language)
trackSelector.setParameters(params)
}
fun onUserLeaveHint() { fun onUserLeaveHint() {
if (usePiP() && shouldStartPiP()) { if (usePiP() && shouldStartPiP()) {
activity?.enterPictureInPictureMode(getPipParams()) activity?.enterPictureInPictureMode(getPipParams())

View File

@ -225,13 +225,15 @@ object PlayerHelper {
true true
) )
val defaultSubtitleCode: String val defaultSubtitleCode: String?
get() { get() {
val code = PreferenceHelper.getString( val code = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE, PreferenceKeys.DEFAULT_SUBTITLE,
"" ""
) )
if (code == "") return null
if (code.contains("-")) { if (code.contains("-")) {
return code.split("-")[0] return code.split("-")[0]
} }