AudioTrack.enabled - Web APIs 编辑

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.

Syntax

isAudioEnabled = AudioTrack.enabled;

AudioTrack.enabled = true | false;

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() {
  var videoElem = document.getElementById("main-video");
  var audioTrackMain;
  var audioTrackCommentary;

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

  if (audioTrackMain && audioTrackCommentary) {
    var 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 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 forEach() method (although the 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 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

SpecificationStatusComment
HTML Living Standard
The definition of 'AudioTrack.enabled' in that specification.
Living Standard 
HTML5
The definition of 'AudioTrack.enabled' in that specification.
Recommendation 

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:115 次

字数:4403

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文