MediaStreamTrackAudioSourceNode

MediaStreamTrackAudioSourceNode interface is a type of AudioNode which represents a source of audio data taken from a specific MediaStreamTrack obtained through the WebRTC or 媒体捕获和流 APIs. The audio itself might be input from a microphone or other audio sampling device, or might be received through a RTCPeerConnection , among other posible options.

A MediaStreamTrackAudioSourceNode has no inputs and exactly one output, and is created using the AudioContext.createMediaStreamTrackSource() method. This interface is similar to MediaStreamAudioSourceNode , except it lets you specifically state the track to use, rather than assuming the first audio track on a stream.

Number of inputs 0
Number of outputs 1
Channel count defined by the first audio MediaStreamTrack passed to the AudioContext.createMediaStreamTrackSource() method that created it.

构造函数

new MediaStreamTrackAudioSourceNode()
创建新的 MediaStreamTrackAudioSourceNode object instance with the specified options.

特性

MediaStreamTrackAudioSourceNode interface has no properties of its own; however, it inherits the properties of its parent, AudioNode .

方法

Inherits methods from its parent, AudioNode .

范例

In this example, we grab a media (audio + video) stream from navigator.getUserMedia , feed the media into a <video> element to play then mute the audio, but then also feed the audio into a MediaStreamAudioSourceNode . Next, we feed this source audio into a low pass BiquadFilterNode (which effectively serves as a bass booster), then a AudioDestinationNode .

The range slider below the <video> element controls the amount of gain given to the lowpass filter — increase the value of the slider to make the audio sound more bass heavy!

注意 : You can see this example running live ,或 view the source .

var pre = document.querySelector('pre');
var video = document.querySelector('video');
var myScript = document.querySelector('script');
var range = document.querySelector('input');
// getUserMedia block - grab stream
// put it into a MediaStreamAudioSourceNode
// also output the visuals into a video element
if (navigator.mediaDevices) {
    console.log('getUserMedia supported.');
    navigator.mediaDevices.getUserMedia ({audio: true, video: true})
    .then(function(stream) {
        video.srcObject = stream;
        video.onloadedmetadata = function(e) {
            video.play();
            video.muted = true;
        };
        // Create a MediaStreamAudioSourceNode
        // Feed the HTMLMediaElement into it
        var audioCtx = new AudioContext();
        var source = audioCtx.createMediaStreamSource(stream);
        // Create a biquadfilter
        var biquadFilter = audioCtx.createBiquadFilter();
        biquadFilter.type = "lowshelf";
        biquadFilter.frequency.value = 1000;
        biquadFilter.gain.value = range.value;
        // connect the AudioBufferSourceNode to the gainNode
        // and the gainNode to the destination, so we can play the
        // music and adjust the volume using the mouse cursor
        source.connect(biquadFilter);
        biquadFilter.connect(audioCtx.destination);
        // Get new mouse pointer coordinates when mouse is moved
        // then set new gain value
        range.oninput = function() {
            biquadFilter.gain.value = range.value;
        }
    })
    .catch(function(err) {
        console.log('The following gUM error occured: ' + err);
    });
} else {
   console.log('getUserMedia not supported on your browser!');
}
// dump script to pre element
pre.innerHTML = myScript.innerHTML;
					

注意 : As a consequence of calling createMediaStreamSource() , audio playback from the media stream will be re-routed into the processing graph of the AudioContext . So playing/pausing the stream can still be done through the media element API and the player controls.

规范

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

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 68 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
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
mediaStreamTrack Chrome 不支持 No Edge 不支持 No Firefox 68 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 68 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。

另请参阅