Skip to main content
Version: 11.7.0

How to dynamically change the visible captions on Roku

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

In the following example we iterate through all available text tracks. Once we find a track with the desired language, we set its mode to showing. After that, we assign the textTracks object back to the THEOplayer attribute. This is all you have to do to make the desired text track visible. You can also change the visible text track during playback.

sub setCaptionsLanguage(language as String)
textTracks = m.player.textTracks
for i = textTracks.count() - 1 to 0 step -1
if m.fullScreen and textTracks[i].kind = "captions" and textTracks[i].language = language then
textTracks[i].mode = "showing"
else
textTracks[i].mode = "hidden"
end if
end for
' Assignment of new roAssociativeArray is required, because Roku field values are copies, not references.
' Read more: https://developer.roku.com/en-gb/docs/developer-program/performance-guide/optimization-techniques.md#OptimizationTechniques-DataFlow
m.player.textTracks = textTracks
end sub