Web 音频 API 's MediaStreamTrackAudioSourceNode() constructor creates and returns a new MediaStreamTrackAudioSourceNode object whose audio is taken from the MediaStreamTrack specified in the given options object.

Another way to create a MediaStreamTrackAudioSourceNode is to call the AudioContext.createMediaStreamTrackSource() method, specifying the MediaStreamTrack from which you want to obtain audio.

句法

audioTrackNode = new MediaStreamTrackAudioSourceNode(context, options);
					

参数

context
AudioContext representing the audio context you want the node to be associated with.
选项

A MediaStreamTrackAudioSourceOptions object defining the properties you want the MediaStreamTrackAudioSourceNode to have:

mediaStreamTrack
MediaStreamTrack from which to take audio data for this node's output.

返回值

新的 MediaStreamTrackAudioSourceNode object representing the audio node whose media is obtained from the specified media track.

异常

NotSupportedError
指定 context is not an AudioContext .
InvalidStateError
指定 MediaStreamTrack isn't an audio track (that is, its kind property isn't audio .

范例

此范例使用 getUserMedia() to obtain access to the user's camera, then creates a new MediaStreamAudioSourceNode from the first audio track provided by the device.

let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (navigator.mediaDevices.getUserMedia) {
  navigator.mediaDevices.getUserMedia (
    {
      audio: true,
      video: false
    }).then(function(stream) {
      let options = {
        mediaStreamTrack: stream.getAudioTracks()[0];
      }
      let source = new MediaStreamTrackAudioSourceNode(audioCtx, options);
      source.connect(audioCtx.destination);
    }).catch(function(err) {
      console.log('The following gUM error occured: ' + err);
    });
} else {
  console.log('new getUserMedia not supported on your browser!');
}
					

规范

规范 状态 注释
Web 音频 API
The definition of 'MediaStreamTrackAudioSourceNode()' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MediaStreamTrackAudioSourceNode() 构造函数 Chrome No Edge No Firefox 68
68
Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 68
68
Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

元数据

  • 最后修改: