refactor: cleanup subtitle logic

This commit is contained in:
Bnyro 2023-08-03 14:24:26 +02:00
parent 6cea53c956
commit 1143e9f2bd

View File

@ -178,11 +178,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
*/ */
private lateinit var chapters: MutableList<ChapterSegment> private lateinit var chapters: MutableList<ChapterSegment>
/**
* for the player view
*/
private var subtitles = mutableListOf<SubtitleConfiguration>()
/** /**
* for the player notification * for the player notification
*/ */
@ -994,12 +989,14 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
} }
}.orEmpty() }.orEmpty()
binding.additionalVideoInfo.text = "${context?.getString(R.string.category)}: ${streams.category}\n" + binding.additionalVideoInfo.text =
"${context?.getString(R.string.category)}: ${streams.category}\n" +
"${context?.getString(R.string.license)}: ${streams.license}\n" + "${context?.getString(R.string.license)}: ${streams.license}\n" +
"${context?.getString(R.string.visibility)}: $visibility" "${context?.getString(R.string.visibility)}: $visibility"
if (streams.tags.isNotEmpty()) { if (streams.tags.isNotEmpty()) {
binding.tagsRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) binding.tagsRecycler.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
binding.tagsRecycler.adapter = VideoTagsAdapter(streams.tags) binding.tagsRecycler.adapter = VideoTagsAdapter(streams.tags)
} }
binding.tagsRecycler.isVisible = streams.tags.isNotEmpty() binding.tagsRecycler.isVisible = streams.tags.isNotEmpty()
@ -1224,10 +1221,15 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
} }
private fun getSubtitleConfigs(): List<SubtitleConfiguration> = streams.subtitles.map {
SubtitleConfiguration.Builder(it.url!!.toUri()).setLanguage(it.code)
.setMimeType(it.mimeType).build()
}
private fun createMediaItem(uri: Uri, mimeType: String) = MediaItem.Builder() private fun createMediaItem(uri: Uri, mimeType: String) = MediaItem.Builder()
.setUri(uri) .setUri(uri)
.setMimeType(mimeType) .setMimeType(mimeType)
.setSubtitleConfigurations(subtitles) .setSubtitleConfigurations(getSubtitleConfigs())
.setMetadata(streams) .setMetadata(streams)
.build() .build()
@ -1260,21 +1262,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
setPreferredAudioRoleFlags(C.ROLE_FLAG_MAIN) setPreferredAudioRoleFlags(C.ROLE_FLAG_MAIN)
} }
// create a list of subtitles
subtitles = mutableListOf()
val subtitlesNamesList = mutableListOf(getString(R.string.none))
val subtitleCodesList = mutableListOf("")
streams.subtitles.forEach {
subtitles.add(
SubtitleConfiguration.Builder(it.url!!.toUri())
.setMimeType(it.mimeType!!) // The correct MIME type (required).
.setLanguage(it.code) // The subtitle language (optional).
.build()
)
subtitlesNamesList += it.name!!
subtitleCodesList += it.code!!
}
// set the default subtitle if available // set the default subtitle if available
updateCaptionsLanguage(captionLanguage) updateCaptionsLanguage(captionLanguage)
@ -1444,18 +1431,14 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
return return
} }
val subtitlesNamesList = mutableListOf(getString(R.string.none)) val subtitles = streams.subtitles.map { it.name!! to it.code!! }
val subtitleCodesList = mutableListOf("") val subtitleOptions = listOf(getString(R.string.none) to "").plus(subtitles)
streams.subtitles.forEach {
subtitlesNamesList += it.name!!
subtitleCodesList += it.code!!
}
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems(subtitlesNamesList) { index -> .setSimpleItems(subtitleOptions.map { it.first }) { index ->
val language = subtitleCodesList.getOrNull(index) val subtitleLanguage = subtitleOptions.getOrNull(index)?.second
updateCaptionsLanguage(language) updateCaptionsLanguage(subtitleLanguage)
this.captionLanguage = language this.captionLanguage = subtitleLanguage
} }
.show(childFragmentManager) .show(childFragmentManager)
} }