fix: captions can't be disabled

This commit is contained in:
Bnyro 2023-08-18 19:48:00 +02:00
parent f84d9b4a17
commit 67b46951d4

View File

@ -168,7 +168,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
*/
private lateinit var exoPlayer: ExoPlayer
private lateinit var trackSelector: DefaultTrackSelector
private var captionLanguage = PlayerHelper.defaultSubtitleCode
private var currentSubtitle = Subtitle(code = PlayerHelper.defaultSubtitleCode)
private val cronetDataSourceFactory = CronetDataSource.Factory(
CronetHelper.cronetEngine,
@ -334,7 +334,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (currentId == transitionEndId) {
viewModel.isMiniPlayerVisible.value = true
// disable captions temporarily
updateCaptionsLanguage(null)
updateCurrentSubtitle(null)
binding.player.useController = false
commentsViewModel.setCommentSheetExpand(null)
binding.sbSkipBtn.isGone = true
@ -343,7 +343,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} else if (currentId == transitionStartId) {
viewModel.isMiniPlayerVisible.value = false
// re-enable captions
updateCaptionsLanguage(captionLanguage)
updateCurrentSubtitle(currentSubtitle)
binding.player.useController = true
commentsViewModel.setCommentSheetExpand(true)
mainMotionLayout.progress = 0F
@ -1212,8 +1212,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
private fun getSubtitleConfigs(): List<SubtitleConfiguration> = streams.subtitles.map {
val roleFlags =
if (it.autoGenerated == true) PlayerHelper.ROLE_FLAG_AUTO_GEN_SUBTITLE else C.ROLE_FLAG_CAPTION
val roleFlags = getSubtitleRoleFlags(it)
SubtitleConfiguration.Builder(it.url!!.toUri())
.setRoleFlags(roleFlags)
.setLanguage(it.code)
@ -1257,7 +1256,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
// set the default subtitle if available
updateCaptionsLanguage(captionLanguage)
updateCurrentSubtitle(currentSubtitle)
// set media source and resolution in the beginning
lifecycleScope.launch(Dispatchers.IO) {
@ -1438,20 +1437,18 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
) { index ->
val subtitle = subtitles.getOrNull(index) ?: return@setSimpleItems
updateCaptionsLanguage(subtitle.code)
trackSelector.updateParameters {
trackSelector.updateParameters {
val roleFlags = if (subtitle.autoGenerated != true) {
updateCurrentSubtitle(subtitle)
this.currentSubtitle = subtitle
}
.show(childFragmentManager)
}
private fun getSubtitleRoleFlags(subtitle: Subtitle?): Int {
return if (subtitle?.autoGenerated != true) {
C.ROLE_FLAG_CAPTION
} else {
PlayerHelper.ROLE_FLAG_AUTO_GEN_SUBTITLE
}
this.setPreferredTextRoleFlags(roleFlags)
}
}
this.captionLanguage = subtitle.code
}
.show(childFragmentManager)
}
override fun onQualityClicked() {
@ -1538,7 +1535,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
binding.linLayout.isGone = true
updateCaptionsLanguage(null)
updateCurrentSubtitle(null)
} else {
// close button got clicked in PiP mode
// pause the video and keep the app alive
@ -1556,7 +1553,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.linLayout.isVisible = true
}
updateCaptionsLanguage(captionLanguage)
updateCurrentSubtitle(currentSubtitle)
binding.optionsLL.post {
binding.optionsLL.requestLayout()
@ -1564,11 +1561,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
}
private fun updateCaptionsLanguage(language: String?) {
trackSelector.updateParameters {
setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
setPreferredTextLanguage(language)
}
private fun updateCurrentSubtitle(subtitle: Subtitle?) = trackSelector.updateParameters {
val roleFlags = if (subtitle?.code != null) getSubtitleRoleFlags(subtitle) else 0
setPreferredTextRoleFlags(roleFlags)
setPreferredTextLanguage(subtitle?.code)
}
fun onUserLeaveHint() {