Add UI support for audio track types

Co-authored-by: AudricV <74829229+AudricV@users.noreply.github.com>
This commit is contained in:
Bnyro 2023-07-16 17:27:10 +02:00
parent c052075380
commit d21daf341d
2 changed files with 53 additions and 8 deletions

View File

@ -1348,9 +1348,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())
trackSelector.updateParameters { trackSelector.updateParameters {
setPreferredAudioLanguage( setPreferredAudioLanguage(LocaleHelper.getAppLocale().isO3Language)
LocaleHelper.getAppLocale().language.lowercase().substring(0, 2)
)
} }
exoPlayer = ExoPlayer.Builder(requireContext()) exoPlayer = ExoPlayer.Builder(requireContext())
@ -1433,21 +1431,59 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
.show(childFragmentManager) .show(childFragmentManager)
} }
private fun getAudioStreamGroups(audioStreams: List<PipedStream>?): Map<String?, List<PipedStream>> { 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<PipedStream>?): Map<String, List<PipedStream>> {
return audioStreams.orEmpty() return audioStreams.orEmpty()
.groupBy { it.audioTrackName } .groupBy { getKeyByAudioStreamGroup(it) }
} }
override fun onAudioStreamClicked() { override fun onAudioStreamClicked() {
val audioGroups = getAudioStreamGroups(streams.audioStreams) val audioGroups = getAudioStreamGroups(streams.audioStreams)
val audioLanguages = audioGroups.map { it.key ?: getString(R.string.default_audio_track) } val audioLanguages = audioGroups.map { it.key }
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems(audioLanguages) { index -> .setSimpleItems(audioLanguages) { index ->
val audioStreams = audioGroups.values.elementAt(index) val audioStreams = audioGroups.values.elementAt(index)
val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2) val firstAudioStream = audioStreams.firstOrNull()
trackSelector.updateParameters { 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) .show(childFragmentManager)

View File

@ -445,6 +445,15 @@
<string name="import_format_youtube_csv">YouTube (CSV)</string> <string name="import_format_youtube_csv">YouTube (CSV)</string>
<string name="home_tab_content">Home tab content</string> <string name="home_tab_content">Home tab content</string>
<string name="show_search_suggestions">Show search suggestions</string> <string name="show_search_suggestions">Show search suggestions</string>
<string name="audio_track_format">%1$s - %2$s</string>
<string name="unknown_audio_language">Unknown audio language</string>
<string name="unknown_audio_track_type">Unknown audio track type</string>
<string name="original_or_main_audio_track">original or main</string>
<string name="dubbed_audio_track">dubbed</string>
<string name="descriptive_audio_track">descriptive</string>
<string name="default_or_unknown_audio_track">default or unknown</string>
<string name="unknown_or_no_audio">unknown or no audio</string>
<!-- Notification channel strings --> <!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string> <string name="download_channel_name">Download Service</string>
<string name="download_channel_description">Shows a notification when downloading media.</string> <string name="download_channel_description">Shows a notification when downloading media.</string>