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

View File

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