Skip to main content
Skip table of contents

Multi-audio

To test this feature and view the example code, please see the Browsers and Connected TV SDK 5 Example Code Quick Start guide.

The CONNECT Player SDK includes built-in multi-audio support. 

Multi-audio here refers to multiple language support only, not to multiple audio bitrates.

Where multiple language tracks are present for a chosen piece of content, they will be added automatically to the player instance.

Taking control

Several events and API calls to are provided to enable the application to take control of audio tracks. For more information, see https://html.spec.whatwg.org/multipage/media.html#audiotracklist.

Getting the list of audio tracks

The player assumes an instance of otvplayer.

player.audioTracks(); returns an AudioTrackList of any AudioTrack objects. For more information, see:

Enabling an audio track

The desired track is enabled by switching the enabled flag to true on the required AudioTrack item in the AudioTrackList array and false to the corresponding track to be deactivated. A change event is fired when a track is enabled or disabled. There will always be a valid audio track playing at any time, even if all track values are set to disabled.

CODE
let myaudiotracks = player.audioTracks(); // assume 2 tracks, first is selected

// set up a listener for the ‘change’ event on the AudioTrackList object
myaudiotracks.addEventListener('change', function() {
  console.log("Change event detected");
});

myaudiotracks[0].enabled = false; // sets enabled to false, but continues to play audio
myaudiotracks[1].enabled = true; // changes audio to the second track

The SDK still handles the addition of the language tracks from the source content, so the following API methods are supplied for completeness:

  • player.audioTracks().addTrack(AudioTrack)adds an existing AudioTrack to the player's internal list of AudioTracks. The addtrack event is fired when a track is added to an AudioTrackList.

  • player.audioTracks().removeTrack(AudioTrack) removes a track from the AudioTrackList currently on the player; if no track exists, it will do nothing. The removetrack event is fired when a track is removed from an AudioTrackList.

Keeping track of the changes

Three callbacks on the AudioTrackList help keep track of when audio tracks are added or removed:

  • The onaddtrack handler is sent a TrackEvent object which indicates in its track property which audio track has been added to the media.

  • The onremovetrack handler is sent a TrackEvent object which indicates in its track property which audio track has been removed from the media.

  • The onchange handler is passed an event in the form of an Event object; the event does not provide any additional information. You must look at their flags to determine the new state of media tracks.

Three associated events: addtrack, removetrack and change can be listened for:

CODE
let myAudioTracks = player.audioTracks();
myAudioTracks.onaddtrack = updateTrackCount;
myAudioTracks.onremovetrack = updateTrackCount;
myAudioTracks.onchange = updateTrackCount;

function updateTrackCount(event) {
  trackCount = myAudioTracks.length;
  // or however you want to handle this
}

myAudioTracks.addEventListener('change', (event) => {
    console.log(`'${event.type}' event fired`);
});

Enabling audio tracks using the preferred audio language

You can configure the audio language preference for DASH content once the player instance is created but before setting the source for the player playback. The player obtains the track information after parsing the manifest file and then activates the audio track in the playback if any one of the available audio tracks is matched to the configured audio preferred language or the first audio track.

The Audio language can be configured using the player configuration preferredAudioLanguage with a value in the form of ISO-639-1 language code (two-letter language code with the optional region subtag) as specified at http://www.iso.org/iso/home/standards/language_codes.htm.

Example code for setting the audio language preference:

CODE
playerInstance.otvtoolkit().configure("preferredAudioLanguage", "es"); // Enable the Spanish Audio track if available
playerInstance.otvtoolkit().configure("preferredAudioLanguage", "en-US"); // Enable the English(United States) Audio track if available
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.