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