[Android] Implement subtitle and audio track language preferences

Kyle Miller git at videolan.org
Tue Nov 2 13:10:20 UTC 2021


vlc-android | branch: master | Kyle Miller <kylemiller3 at my.unt.edu> | Fri Oct  8 08:48:41 2021 +0200| [9c45f26d8b424294dd0bfa81e4faf52e168ae8af] | committer: Nicolas Pomepuy

Implement subtitle and audio track language preferences

Fixes #577
Fixes #1416

> https://code.videolan.org/videolan/vlc-android/commit/9c45f26d8b424294dd0bfa81e4faf52e168ae8af
---

 .../resources/src/main/res/values/strings.xml      |  2 ++
 .../vlc-android/res/xml/preferences_audio.xml      |  6 ++++
 .../vlc-android/res/xml/preferences_subtitles.xml  |  5 ++++
 .../videolan/vlc/gui/video/VideoPlayerActivity.kt  | 34 +++++++++++++++++++---
 4 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index 9eb76d383..1fb6f5965 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -291,6 +291,7 @@
     <string name="screen_orientation_last_locked">Last locked orientation</string>
     <string name="enable_black_theme">Black theme</string>
     <string name="subtitle_text_encoding">Subtitle text encoding</string>
+    <string name="subtitle_preferred_language">Preferred subtitle language</string>
     <string name="daynight_title">DayNight mode</string>
     <string name="daynight_follow_system_title">Follow system mode</string>
 
@@ -590,6 +591,7 @@
     <string name="audio_digital_failed">Failed to change Audio Digital Output state</string>
     <string name="audio_digital_output_enabled">Audio Digital Output enabled</string>
     <string name="audio_digital_output_disabled">Audio Digital Output disabled</string>
+    <string name="audio_preferred_language">Preferred audio language</string>
     <string name="audio_digital_title">Digital audio output (passthrough)</string>
     <string name="audio_task_cleared_title">Stop on application swipe</string>
     <string name="audio_task_cleared_summary">Stop playback when application is dismissed</string>
diff --git a/application/vlc-android/res/xml/preferences_audio.xml b/application/vlc-android/res/xml/preferences_audio.xml
index 2cc8a86e6..961a7f168 100644
--- a/application/vlc-android/res/xml/preferences_audio.xml
+++ b/application/vlc-android/res/xml/preferences_audio.xml
@@ -31,6 +31,12 @@
             android:key="audio_digital_output"
             android:title="@string/audio_digital_title"/>
 
+    <EditTextPreference
+        android:defaultValue=""
+        android:key="audio_preferred_language"
+        android:persistent="true"
+        android:title="@string/audio_preferred_language"/>
+
     <PreferenceCategory
             android:title="@string/headset_prefs_category"
             android:key="headset_prefs_category">
diff --git a/application/vlc-android/res/xml/preferences_subtitles.xml b/application/vlc-android/res/xml/preferences_subtitles.xml
index 5c053410d..bfd303a83 100644
--- a/application/vlc-android/res/xml/preferences_subtitles.xml
+++ b/application/vlc-android/res/xml/preferences_subtitles.xml
@@ -41,5 +41,10 @@
             android:key="subtitle_text_encoding"
             android:persistent="true"
             android:title="@string/subtitle_text_encoding"/>
+    <EditTextPreference
+            android:defaultValue=""
+            android:key="subtitle_preferred_language"
+            android:persistent="true"
+            android:title="@string/subtitle_preferred_language"/>
 
 </androidx.preference.PreferenceScreen>
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index d704cdcd9..afa89e311 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -1254,20 +1254,46 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
                         if (event.esChangedType == IMedia.Track.Type.Audio) {
                             lifecycleScope.launch(Dispatchers.IO) {
                                 val media = medialibrary.findMedia(mw)
-                                val audioTrack = media.getMetaLong(MediaWrapper.META_AUDIOTRACK).toInt()
+                                var preferredTrack: Int = 0
+                                val contains = settings.getString("audio_preferred_language", null)
+                                val it = PlaybackService.instance?.audioTracks?.iterator()
+                                while (it?.hasNext() === true) {
+                                    val next = it?.next()
+                                    if (next.name.contains(contains.toString(), ignoreCase = true)) {
+                                        preferredTrack = next.id
+                                        break
+                                    }
+                                }
+                                val audioTrack = when (val savedTrack = media.getMetaLong(MediaWrapper.META_AUDIOTRACK).toInt()) {
+                                    0 -> preferredTrack
+                                    else -> savedTrack
+                                }
                                 if (audioTrack != 0 || currentAudioTrack != -2)
-                                    service.setAudioTrack(if (media.id == 0L) currentAudioTrack else audioTrack)
+                                    service.setAudioTrack(audioTrack)
                             }
                         } else if (event.esChangedType == IMedia.Track.Type.Text) {
                             lifecycleScope.launch(Dispatchers.IO) {
                                 val media = medialibrary.findMedia(mw)
-                                val spuTrack = media.getMetaLong(MediaWrapper.META_SUBTITLE_TRACK).toInt()
+                                var preferredTrack: Int = 0
+                                val contains = settings.getString("subtitle_preferred_language", null)
+                                val it = PlaybackService.instance?.spuTracks?.iterator()
+                                while (it?.hasNext() === true) {
+                                    val next = it?.next()
+                                    if (next.name.contains(contains.toString(), ignoreCase = true)) {
+                                        preferredTrack = next.id
+                                        break
+                                    }
+                                }
+                                val spuTrack = when (val savedTrack = media.getMetaLong(MediaWrapper.META_SUBTITLE_TRACK).toInt()) {
+                                    0 -> preferredTrack
+                                    else -> savedTrack
+                                }
                                 if (addNextTrack) {
                                     val tracks = service.spuTracks
                                     if (!(tracks as Array<MediaPlayer.TrackDescription>).isNullOrEmpty()) service.setSpuTrack(tracks[tracks.size - 1].id)
                                     addNextTrack = false
                                 } else if (spuTrack != 0 || currentSpuTrack != -2) {
-                                    service.setSpuTrack(if (media.id == 0L) currentSpuTrack else spuTrack)
+                                    service.setSpuTrack(spuTrack)
                                     lastSpuTrack = -2
                                 }
                             }



More information about the Android mailing list