Skip to main content
Version: 4.12.7

How to programmatically detect audio tracks

This article describes how you can use the API to detect audio tracks.

The AudioTrack API, which is a sub-API of the MediaTrack 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 audio languages.

SDKs

Web SDKAndroid SDKiOS SDKtvOS SDKAndroid TV SDKChromecast SDKRoku SDK
YesYesYesYesYesYesYes

Code examples

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

Web SDK

The Web SDK leverages the MediaTrack API.

function handleAddTrackEvent(addTrackEvent) {
var audioTrack = addTrackEvent.track;
}
player.audioTracks.addEventListener('addtrack', handleAddTrackEvent);
Android (TV) SDK

The Android SDK leverages the MediaTrack API.

EventListener<AddTrackEvent> handleAddTrackEvent= new EventListener<AddTrackEvent>() {
@Override
public void handleEvent(AddTrackEvent addTrackEvent) {
MediaTrack audioTrack = addTrackEvent.getTrack();
}
};
tpv.getPlayer().getAudioTracks().addEventListener(AudioTrackListEventTypes.ADDTRACK, handleAddTrackEvent);
iOS (/tvOS) SDK

The iOS SDK leverages the MediaTrack API.

func handleAddTrackEvent(addTrackEvent : AddTrackEvent) {
var audioTrack = addTrackEvent .track
}
self.theoplayer.audioTracks.addEventListener(type: AudioTrackListEventTypes.ADD_TRACK, listener: handleAddTrackEvent)
Roku SDK

Snippet below allows you to detect added audio tracks. In this example, we have used the addedaudiotrack event to propagate information about audio tracks. Basically, all we have to do is to add an event listener.

function Init()
...
m.THEOplayer.callFunc("addEventListener", "addedaudiotrack", "onAudioTracksChanged")
...
end function

function onAudioTracksChanged()
audioTracks = m.player.audioTracks
? "Audio tracks changed."
? audioTracks
end function

Remarks