How to dynamically change the visible captions on Android
This article describes how you can dynamically change the currently selected subtitle or closed captions language, or how you can set a default language.
This article assumes that you are using text tracks that are loaded with the manifest. If you add the text track separately, you can also mark it as the default track when you configure your source.
The examples below match tracks on their language property, which contains the ISO language code of the track.
If that is not sufficient for your use case, for example when a stream contains multiple tracks with the same language,
you can match on the track's label instead, which contains the human-readable name shown in your UI.
Usage
The function below disables all text tracks which are currently active, and then enables the text track with the requested language.
fun setLanguage(player: Player, language: String) {
for (textTrack in player.textTracks) {
textTrack.mode = if (textTrack.language == language) {
TextTrackMode.SHOWING
} else {
TextTrackMode.DISABLED
}
}
}