Skip to main content
Version: 6.13.0

How to programmatically detect text tracks

This article describes how you can use the API to detect text tracks, which can be subtitles, closed captions or metadata.

The TextTrack 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 visualize the available text languages.

SDKs

Web SDKAndroid SDKiOS SDKtvOS SDKAndroid TV SDKChromecast SDKRoku SDK
YesYesYesYesYesYesYes

Code examples

The code examples below showcase how to implement the detection of audio tracks across SDKs.

Web SDK

The Web SDK leverages the TextTrack API.

function handleAddTrackEvent(addTrackEvent) {
var textTrack = addTrackEvent.track;
}
player.textTracks.addEventListener("addtrack", handleAddTrackEvent);
Android (TV) SDK

The Android SDK leverages the TextTrack API.

EventListener<AddTrackEvent> handleAudioTrackAdd = new EventListener<AddTrackEvent>() {
@Override
public void handleEvent(AddTrackEvent addTrackEvent) {
TextTrack textTrack = addTrackEvent.getTrack();
}
};
tpv.getPlayer().getTextTracks().addEventListener(TextTrackListEventTypes.ADDTRACK, handleAudioTrackAdd);
iOS (/tvOS) SDK

The iOS SDK leverages the MediaTrack API.

func handleAddTrackEvent(addTrackEvent : AddTrackEvent) {
var textTrack = addTrackEvent.track
}
self.theoplayer.textTracks.addEventListener(type: TextTrackListEventTypes.ADD_TRACK, listener: handleAddTrackEvent)
Roku SDK

To obtain an exact moment of recognizing text track we may use an “addedtexttrack” event. This will allow to programmatically detect all text tracks which are loaded with the stream.

function Init(){
...
m.THEOplayer.callFunc("addEventListener", "addedtexttrack", "onEventTextTracksChanged")
...
end function

function onEventTextTracksChanged()
textTracks= m.player.textTracks
? "Text tracks changed."
? textTracks
end function

Remarks