Version: 2.67.0

Using MediaTrack API | How to programmatically enable or disable audio tracks

This article describes how you can use the API to enable or disable 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 toggle audio languages.

Table of contents

SDKs

Web SDKAndroid SDKAndroid TV SDKiOS SDKtvOS Chromecast SDK YesYesYesYesYesYes

Code examples

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

Web SDK

// disable all audio tracks
player.audioTracks.forEach(function(track) {
track.enabled = false;
});
// enable a specific audio track
player.audioTracks[indexOfRequestedAudioTrack].enabled = true;

Android (TV) SDK

// disable all audio tracks
for (int i = 0; i < theoplayer.getPlayer().getAudioTracks().length(); i++) {
theoplayer.getPlayer().getAudioTracks().getItem(i).setEnabled(false);
}
// enable a specific audio track
theoplayer.getPlayer().getAudioTracks().getItem(i).setEnabled(true);

iOS (/tvOS) SDK

// disable all audio tracks
for i in 0..<self.player.audioTracks.count {
self.player.audioTracks[i].enabled = false
}
// enable a specific audio track
self.player.audioTracks[indexOfRequestedAudioTrack].enabled = true

Remarks

Related articles

  • Page:

How to programmatically detect video track qualities

  • Page:

How to programmatically select a video track quality

  • Page:

How to programmatically detect audio tracks