MediaStreamAudioSourceNode
interface is a type of
AudioNode
which operates as an audio source whose media is received from a
MediaStream
obtained using the WebRTC or Media Capture and Streams APIs.
This media could be from a microphone (through
getUserMedia()
) or from a remote peer on a WebRTC call (using the
RTCPeerConnection
's audio tracks).
A
MediaStreamAudioSourceNode
has no inputs and exactly one output, and is created using the
AudioContext.createMediaStreamSource()
方法。
MediaStreamAudioSourceNode
takes the audio from the
第一
MediaStreamTrack
whose
kind
attribute's value is
audio
。见
Track ordering
for more information about the order of tracks.
The number of channels output by the node matches the number of tracks found in the selected audio track.
| 输入数 |
0
|
|---|---|
| 输出数 |
1
|
| 通道计数 |
defined by the first audio
MediaStreamTrack
passed to the
AudioContext.createMediaStreamSource()
method that created it.
|
new MediaStreamAudioSourceNode()
MediaStreamAudioSourceNode
object instance with the specified options.
In addition to the following properties,
MediaStreamAudioSourceNode
inherits the properties of its parent,
AudioNode
.
mediaStream
只读
MediaStream
used when constructing this
MediaStreamAudioSourceNode
.
继承方法来自其父级
AudioNode
.
InvalidStateError
mediaStream
parameter does not contain any audio tracks.
Typically, you should probably not use this type of node. It has been replaced with the more predictable
MediaStreamTrackAudioSourceNode
, which has better-defined rules for how it chooses the track to output.
If you do chooose to use
MediaStreamAudioSourceNode
, you should keep the following in mind.
For the purposes of the
MediaStreamTrackAudioSourceNode
interface, the order of the audio tracks on the stream is determined by taking the tracks whose
kind
is
audio
, then sorting the tracks by their
id
property's values, in Unicode code point order (essentially, in alphabetical or lexicographical order, for IDs which are simple alphanumeric strings).
第一
track, then, is the track whose
id
comes first when the tracks' IDs are all sorted by Unicode code point.
However, it's important to note that the rule establishing this ordering was added long after this interface was first introduced into the
Web 音频 API
. As such, you can't easily rely on the order matching between any two browsers or browser versions. This is why it is typically wiser to use
MediaStreamTrackAudioSourceNode
, which provides similar capabilities but was better-defined upon being added to the specification, so it's more reliable.
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.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'MediaStreamAudioSourceNode' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
MediaStreamAudioSourceNode
|
Chrome 23 | Edge ≤18 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android Yes | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android Yes |
MediaStreamAudioSourceNode()
构造函数
|
Chrome 55 | Edge ≤79 | Firefox 53 | IE 不支持 No | Opera 42 | Safari ? |
WebView Android
55
注意事项
|
Chrome Android
55
注意事项
|
Firefox Android 53 | Opera Android 42 | Safari iOS ? |
Samsung Internet Android
6.0
注意事项
|
mediaStream
|
Chrome 23 | Edge ≤79 | Firefox 70 | IE 不支持 No | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 不支持 No | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
见实现注意事项。
MediaStreamTrackAudioSourceNode
MediaStreamAudioSourceNode
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode