How to programmatically detect audio track changes on Web
This article describes how you can use the AudioTrack API to detect audio track changes. An audio track "change" is triggered by enabling (or disabling) an audio track.
Implementing this functionality is a common use-case for developers who want to build their own UI, and annotate the audio track that is currently active.
Usage
The implementation of the Web SDK applies to all web-based platforms, including Tizen and webOS.
The Web SDK exposes the AudioTrack API through player.audioTracks.
This audioTracks property is a MediaTrackList that inherits from the TrackList.
This TrackList dispatches the events from the TrackListEventMap.
This TrackListEventMap contains the change event, as well as the addtrack and removetrack event.
The code below allows you to detect audio track changes.
player.audioTracks.addEventListener('change', function (event) {
const track = event.track;
console.log(track, track.label, track.language, track.enabled);
});
The properties of a media track (e.g. enabled, language) are described in the MediaTrack API reference.
Related articles
Are you reading this article because you are interested in audio tracks? Continue reading below.
Are you reading this article because you are implementing a custom UI? Then you will find the following articles interesting: