From d21daf341d3274c41d2355c144caa78d19cd71ef Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 16 Jul 2023 17:27:10 +0200 Subject: [PATCH] Add UI support for audio track types Co-authored-by: AudricV <74829229+AudricV@users.noreply.github.com> --- .../libretube/ui/fragments/PlayerFragment.kt | 52 ++++++++++++++++--- app/src/main/res/values/strings.xml | 9 ++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 78637cc55..ad75a953a 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1348,9 +1348,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { trackSelector = DefaultTrackSelector(requireContext()) trackSelector.updateParameters { - setPreferredAudioLanguage( - LocaleHelper.getAppLocale().language.lowercase().substring(0, 2) - ) + setPreferredAudioLanguage(LocaleHelper.getAppLocale().isO3Language) } exoPlayer = ExoPlayer.Builder(requireContext()) @@ -1433,21 +1431,59 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { .show(childFragmentManager) } - private fun getAudioStreamGroups(audioStreams: List?): Map> { + private fun getDisplayTrackType(trackType: String?): String { + if (trackType == null) { + return getString(R.string.unknown_audio_track_type) + } + + return when (trackType.lowercase()) { + "descriptive" -> getString(R.string.descriptive_audio_track) + "dubbed" -> getString(R.string.dubbed_audio_track) + "original" -> getString(R.string.original_or_main_audio_track) + else -> getString(R.string.unknown_audio_track_type) + } + } + + private fun getKeyByAudioStreamGroup(audioStream: PipedStream): String { + val nullAudioLocale = audioStream.audioTrackLocale == null + if (nullAudioLocale && audioStream.audioTrackType == null) { + // A track without a locale set and a track type should be a default track + return getString(R.string.default_audio_track) + } + + return getString(R.string.audio_track_format).format( + if (nullAudioLocale) getString(R.string.unknown_audio_language) + else Locale.forLanguageTag(audioStream.audioTrackLocale!!) + .getDisplayLanguage(LocaleHelper.getAppLocale()) + .ifEmpty { getString(R.string.unknown_audio_language) }, + getDisplayTrackType(audioStream.audioTrackType)) + } + + private fun getAudioStreamGroups(audioStreams: List?): Map> { return audioStreams.orEmpty() - .groupBy { it.audioTrackName } + .groupBy { getKeyByAudioStreamGroup(it) } } override fun onAudioStreamClicked() { val audioGroups = getAudioStreamGroups(streams.audioStreams) - val audioLanguages = audioGroups.map { it.key ?: getString(R.string.default_audio_track) } + val audioLanguages = audioGroups.map { it.key } BaseBottomSheet() .setSimpleItems(audioLanguages) { index -> val audioStreams = audioGroups.values.elementAt(index) - val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2) + val firstAudioStream = audioStreams.firstOrNull() trackSelector.updateParameters { - setPreferredAudioLanguage(lang) + setPreferredAudioLanguage(firstAudioStream?.audioTrackLocale) + setPreferredAudioRoleFlags( + if (firstAudioStream?.audioTrackType == null) { + C.ROLE_FLAG_MAIN + } else when (firstAudioStream.audioTrackType.lowercase()) { + "descriptive" -> C.ROLE_FLAG_DESCRIBES_VIDEO + "dubbed" -> C.ROLE_FLAG_DUB + "original" -> C.ROLE_FLAG_MAIN + else -> C.ROLE_FLAG_ALTERNATE + } + ) } } .show(childFragmentManager) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 44008ad86..19e16dfcc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -445,6 +445,15 @@ YouTube (CSV) Home tab content Show search suggestions + %1$s - %2$s + Unknown audio language + Unknown audio track type + original or main + dubbed + descriptive + default or unknown + unknown or no audio + Download Service Shows a notification when downloading media.