AudioTrack

AudioTrack 接口表示从HTML介质元件中的一个单一的音轨, <audio><video>. 访问AudioTrack 对象的最常见用途是切换其enabled 属性,以使轨道静音和取消静音。

属性

enabled
一个布尔值,用于控制是否启用音轨的声音。设置此值false可使音轨的音频静音。
id 只读
一个DOMString唯一标识媒体中的曲目。此ID可用于通过调用AudioTrackList.getTrackById()来定位音轨列表中的特定轨道。如果媒体支持按媒体片段URI规范按媒体片段搜索,则ID也可以用作URL的片段部分。
kind 只读
一个DOMString指定轨道所属的类别。例如,主音频轨道将有一个kind"main"
label 只读
A DOMString为轨道提供人类可读的标签。例如,一个音频评论轨道的电影可以有一个label"Commentary with director John Q. Public and actors John Doe and Jane Eod.",如果没有提供标签此字符串是空的。
language 只读
一个DOMString指定音轨的主要语言,如果未知,则为空字符串。该语言被指定为BCP 47(RFC 5646}语言代码,例如"en-US""pt-BR"
sourceBuffer 只读
创建轨道的SourceBuffer。如果轨道不是由SourceBuffer创建的,或者SourceBuffer已从MediaSource.sourceBuffers属性中删除,则返回null其母媒体来源。

使用说明

要获取AudioTrack给定媒体元素,请使用元素的audioTracks属性,该属性返回AudioTrackList对象,您可以从中获取媒体中包含的各个曲目:

var el = document.querySelector("video");
var tracks = el.audioTracks;

然后,您可以使用数组语法或forEach()等函数访问媒体的各个轨道。

第一个示例获取媒体上的第一个音轨:

var firstTrack = tracks [0];

下一个示例扫描所有媒体的音轨,启用用户首选语言中的任何一种(取自变量userLanguage)并禁用任何其他语音。

tracks.forEach(function(track){
  if(track.language === userLanguage){
    track.enabled = true;
  } else {
    track.enabled = false;
  }
});

language采用标准(RFC 5646)格式。例如,对于美国英语,这将是"en-US"

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.

产品规格

规格 状态 评论
HTML Living Standard
AudioTrack
Living Standard  
HTML5
AudioTrack
Recommendation  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
AudioTrackChrome ? Edge Full support YesFirefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
enabledChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
idChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
kindChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
labelChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
languageChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
sourceBufferChrome ? Edge Full support 12Firefox ? IE ? Opera ? Safari Full support YesWebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown