Version: 2.67.0

How to programmatically enable or disable text tracks?

This article describes how you can use the API to enable or disable subtitles, closed captions or metadata.

The TextTracks API can be used to implement this functionality. Implementing this functionality is a common use-case for developers who want to build their own UI to toggle subtitles.

SDK

Web SDKAndroid SDKiOS SDKtvOSAndroid TV SDKChromecast SDK
YesYesYesYesYesYes

Code examples

The code examples below how to implement toggling subtitles across SDK. It's advised to disable text tracks you don't want to display, in order to avoid issues with overlapping text.

Web SDK

// disable all text tracks
player.textTracks.forEach(function(track) {
track.mode = "disabled";
});
// enable a specific text track
player.textTracks[indexOfRequestedTextTrack].mode = "showing";

Android (TV) SDK

// disable all text tracks
for (int i = 0; i < tpv.getPlayer().getTextTracks().length(); i++) {
theoplayer.getPlayer().getTextTracks().getItem(i).setMode(TextTrackMode.DISABLED);
}
// enable a specific text track
theoplayer.getPlayer().getTextTracks().getItem(indexOfRequestedTextTrack).setMode(TextTrackMode.SHOWING);

iOS (/tvOS) SDK

// disable all text tracks
for i in 0..<self.player.textTracks.count {
var track = self.player.textTracks[i].mode = TextTrackMode.disabled
}
// enable a specific text track
self.player.textTracks[indexOfRequestedTextTrack].mode = TextTrackMode.showing

Remarks