MediaStream.getAudioTracks()

MediaStream 接口下的 getAudioTracks() 方法会返回一个包含 track set 流中所有 MediaStreamTrack.kind 为 audio 的 MediaStreamTrack 对象序列。

语法

var mediaStreamTracks = mediaStream.getAudioTracks()

参数

返回值

MediaStreamTrack 对象数组,包含流中所有的音轨。音轨的 kind 值为 audio 。如果流中不包含音轨,则数组为空。

注意:数组中返回的顺序并不是由规范定义的,事实上,每次调用 getAudioTracks() 的结果都可能有所不同。

更早版本的本API中,包含一个用做列表中每个音频类型的 AudioStreamTrack 接口;现在已被合并至 MediaStreamTrack 主接口中。

示例

本示例使用 getUserMedia() 获取视频流中的网络摄像机的音频和视频,并将媒体流绑定到 <video> 元素,然后设置一个计时器,计时器到期时会停止在该媒体流中找到的第一个音轨。

navigator.mediaDevices.getUserMedia({audio: true, video: true})
.then(mediaStream => {
  document.querySelector('video').srcObject = mediaStream;
  // Stop the audio stream after 5 seconds
  setTimeout(() => {
    const tracks = mediaStream.getAudioTracks()
    tracks[0].stop()
  }, 5000)
})

说明

说明 状态 评论
Media Capture and Streams
getAudioTracks()
Candidate Recommendation Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
getAudioTracksChrome Full support 26Edge Full support 12Firefox Full support 22
Notes
Full support 22
Notes
Notes Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support 37Chrome Android Full support 26Firefox Android Full support 22
Notes
Full support 22
Notes
Notes Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
Opera Android No support NoSafari iOS Full support YesSamsung Internet Android Full support 1.5

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.