大网数据广告活动
Web 标准与前端开发

AudioTrack: enabled property

· 52CE 我爱测

The **AudioTrack** property **`enabled`** specifies whether or not the described audio track is currently enabled for use. If the track is disabled by setting `enabled` to `false`, the track is muted and does not produce audio.

HTML DOM

The AudioTrack property enabled specifies whether or not the described audio track is currently enabled for use. If the track is disabled by setting enabled to false, the track is muted and does not produce audio.

Value

The enabled property is a Boolean whose value is true if the track is enabled; enabled tracks produce audio while the media is playing. Setting enabled to false effectively mutes the audio track, preventing it from contributing to the media's audio performance.

Example

This example switches between the main and commentary audio tracks of a media element.

function swapCommentaryMain() {
  const videoElem = document.getElementById("main-video");
  let audioTrackMain;
  let audioTrackCommentary;

  videoElem.audioTracks.forEach((track) => {
    if (track.kind === "main") {
      audioTrackMain = track;
    } else if (track.kind === "commentary") {
      audioTrackCommentary = track;
    }
  });

  if (audioTrackMain && audioTrackCommentary) {
    const commentaryEnabled = audioTrackCommentary.enabled;
    audioTrackCommentary.enabled = audioTrackMain.enabled;
    audioTrackMain.enabled = commentaryEnabled;
  }
}

The swapCommentaryMain() function above finds within the audio tracks of the video element "main-video" the audio tracks whose AudioTrack.kind values are "main" and "commentary". These represent the primary audio track and the commentary track.

[!NOTE] This example assumes that there is only one of each kind of track in the video, but this is not necessarily the case.

The element's audio tracks are then scanned through using the JavaScript Array.forEach method (although the HTMLMediaElement.audioTracks property of a media element isn't actually a JavaScript array, it can be accessed like one for the most part).

The scan looks for the tracks whose AudioTrack.kind values are "main" and "commentary" and remembers those AudioTrack objects. Once those have been found, the values of the two tracks' enabled properties are exchanged, which results in swapping which of the two tracks is currently active.

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。