您正位於 MDN Web Docs 的可編輯版本

以 MDN Web Docs 使用者身分檢視: https://developer.mozilla.org/zh-TW/docs/Web/API/AudioTrack

我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

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.

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 DOMString 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 DOMString specifying the category into which the track falls. For example, the main audio track would have a kind of "main".
label Read only
A DOMString 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 DOMString 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:

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.

Example

This example returns an array of track kinds and labels for potential use in a user interface to select audio tracks for a specified media element. The list is filtered to only allow certain track kinds through.

function getTrackList(el) {
  var trackList = [];
  const wantedKinds = [
    "main", "alternative", "main-desc", "translation", "commentary"
  ];

  el.audioTracks.forEach(function(track) {
    if (wantedKinds.includes(track.kind)) {
      trackList.push({
        id: track.id,
        kind: track.kind,
        label: track.label
      });
    }
  });
  return trackList;
}

The resulting trackList contains an array of audio tracks whose kind is one of those in the array wantedKinds, with each entry providing the track's id, kind, and label.

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'AudioTrack' in that specification.
Living Standard  
HTML5
The definition of 'AudioTrack' in that specification.
Recommendation  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
AudioTrackChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
enabledChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
idChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
kindChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
labelChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
languageChrome Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 10Opera Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 37
Disabled
Full support 37
Disabled
Disabled From version 37: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 33
Disabled
Full support 33
Disabled
Disabled From version 33: this feature is behind the media.track.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24
Disabled
Full support 24
Disabled
Disabled From version 24: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No
sourceBufferChrome Full support 51
Disabled
Full support 51
Disabled
Disabled From version 51: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Disabled
Full support 79
Disabled
Disabled From version 79: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
No support 12 — 79
Firefox No support NoIE Full support 11Opera Full support 38
Disabled
Full support 38
Disabled
Disabled From version 38: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari Full support 6.1WebView Android No support NoChrome Android Full support 51
Disabled
Full support 51
Disabled
Disabled From version 51: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android No support NoOpera Android Full support 41
Disabled
Full support 41
Disabled
Disabled From version 41: this feature is behind the enable-experimental-web-platform-features preference (needs to be set to enabled).
Safari iOS Full support 7Samsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

文件標籤與貢獻者

此頁面的貢獻者: mdnwebdocs-bot, Sheppy, sideshowbarker
最近更新: mdnwebdocs-bot,