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.
Instance properties
-
enabled
-
A Boolean value which controls whether or not the audio track's sound is enabled. Setting this value to false
mutes the track's audio.
id
Read only
-
A 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 the Media Fragments URI specification.
kind
Read only
-
A string specifying the category into which the track falls. For example, the main audio track would have a kind
of "main"
.
label
Read only
-
A string providing a human-readable label for the track. For example, an audio commentary track for a movie might have a label
of "Commentary with director John Q. Public and actors John Doe and Jane Eod."
This string is empty if no label is provided.
language
Read only
-
A string specifying the audio track's primary language, or an empty string if unknown. The language is specified as a BCP 47 (RFC 5646) language code, such as "en-US"
or "pt-BR"
.
sourceBuffer
Read only
-
The SourceBuffer
that created the track. Returns null if the track was not created by a SourceBuffer
or the SourceBuffer
has been removed from the MediaSource.sourceBuffers
attribute of its parent media source.
Usage notes
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:
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 as forEach()
.
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 variable userLanguage
) and disabling any others.
tracks.forEach((track) => {
track.enabled = track.language === userLanguage;
});
The language
is in standard (RFC 5646) format. For US English, this would be "en-US"
, for example.
Example
See AudioTrack.label
for a simple example that shows how to get an array of track kinds and labels for a specified media element, filtered by kind.
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
AudioTrack |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
enabled |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
id |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
kind |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
label |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
language |
37 |
7912–79 |
33 |
10 |
24 |
8 |
No |
37 |
No |
No |
8 |
No |
sourceBuffer |
51 |
7912–79 |
No |
11 |
38 |
8 |
No |
51 |
No |
No |
13Exposed in Mobile Safari on iPad but not on iPhone.
|
No |