The AudioTrack
interface represents a single audio track from one of the HTML media elements, <audio>
or <video>
.
The most common use for accessing an AudioTrack
object is to toggle its enabled
property in order to mute and unmute the track.
To get an AudioTrack
for a given media element, use the element's audioTracks
property, which returns an AudioTrackList
object from which you can get the individual tracks contained in the media:
var el = document.querySelector("video");
var tracks = el.audioTracks;
You can then access the media's individual tracks using either array syntax or functions such as forEach()
.
This first example gets the first audio track on the media:
var firstTrack = tracks[0];
The next example scans through all of the media's audio tracks, enabling any that are in the user's preferred language (taken from a variable userLanguage
) and disabling any others.
tracks.forEach(function(track) {
if (track.language === userLanguage) {
track.enabled = true;
} else {
track.enabled = false;
}
});
The language
is in standard (RFC 5646) format. For US English, this would be "en-US"
, for example.
See AudioTrack.label
for a simple example that shows how to get a array of track kinds and labels for a specified media element, filtered by kind.