Skip to content

Commit

Permalink
Merge pull request #4540 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: captions can't be disabled
  • Loading branch information
Bnyro authored Aug 18, 2023
2 parents 197c143 + 67b4695 commit afe635a
Showing 1 changed file with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1438,22 +1437,20 @@ 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) {
C.ROLE_FLAG_CAPTION
} else {
PlayerHelper.ROLE_FLAG_AUTO_GEN_SUBTITLE
}
this.setPreferredTextRoleFlags(roleFlags)
}
}
this.captionLanguage = subtitle.code
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
}
}

override fun onQualityClicked() {
// get the available resolutions
val resolutions = getAvailableResolutions()
Expand Down Expand Up @@ -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
Expand All @@ -1556,19 +1553,18 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.linLayout.isVisible = true
}

updateCaptionsLanguage(captionLanguage)
updateCurrentSubtitle(currentSubtitle)

binding.optionsLL.post {
binding.optionsLL.requestLayout()
}
}
}

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() {
Expand Down

0 comments on commit afe635a

Please sign in to comment.