AudioTrack
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
TheAudioTrack interface represents a single audio track from one of the HTML media elements,<audio> or<video>.
The most common use for accessing anAudioTrack object is to toggle itsenabled property in order to mute and unmute the track.
In this article
Instance properties
enabledA Boolean value which controls whether or not the audio track's sound is enabled. Setting this value to
falsemutes the track's audio.idRead onlyA string which uniquely identifies the track within the media. This ID can be used to locate a specific track within an audio track list by calling
AudioTrackList.getTrackById(). The ID can also be used as the fragment part of the URL if the media supports seeking by media fragment per theMedia Fragments URI specification.kindRead onlyA string specifying the category into which the track falls. For example, the main audio track would have a
kindof"main".labelRead onlyA string providing a human-readable label for the track. For example, an audio commentary track for a movie might have a
labelof"Commentary with director Christopher Nolan and actors Leonardo DiCaprio and Elliot Page."This string is empty if no label is provided.languageRead onlyA string specifying the audio track's primary language, or an empty string if unknown. The language is specified as aBCP 47 language tag, such as
"en-US"or"pt-BR".sourceBufferRead onlyThe
SourceBufferthat created the track. Returns null if the track was not created by aSourceBufferor theSourceBufferhas been removed from theMediaSource.sourceBuffersattribute of its parent media source.
Usage notes
To get anAudioTrack for a given media element, use the element'saudioTracks property, which returns anAudioTrackList object from which you can get the individual tracks contained in the media:
const el = document.querySelector("video");const tracks = el.audioTracks;You can then access the media's individual tracks using either array syntax or functions such asforEach().
This first example gets the first audio track on the media:
const 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 variableuserLanguage) and disabling any others.
tracks.forEach((track) => { track.enabled = track.language === userLanguage;});Thelanguage is specified as a validBCP 47 language tag, for example"en-US" for US English.
Example
SeeAudioTrack.label for an example that shows how to get an array of track kinds and labels for a specified media element, filtered by kind.
Specifications
| Specification |
|---|
| HTML> # audiotrack> |