The MediaStreamConstraints
dictionary's audio
property is used to indicate what kind of audio track, if any, should be included in the MediaStream
returned by a call to getUserMedia()
.
To learn more about how constraints work, see Capabilities, constraints, and settings.
var audioConstraints = true | false | MediaTrackConstraints;
The value of the audio
property can be specified as either of two types:
Boolean
true
, an audio track is included; if no audio source is available or if permission is not given to use the audio source, the call to getUserMedia()
will fail. If false
, no audio track is included.MediaTrackConstraints
In this example, we provide a simple value of true
for the audio
property. This tells getUserMedia()
that we require an audio track, but we don't care about any specifics beyond that.
document.getElementById("startButton").addEventListener("click", function() { navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => audioElement.srcObject = stream) .catch(err => log(err.name + ": " + err.message)); }, false);
Here we see an event handler for a click
event which uses getUserMedia()
to obtain an audio-only stream with no specific constraints, then attaches the resulting stream to an <audio>
element once the stream is returned.
Now let's look at a similar example that uses a set of constraints based on the MediaTrackConstraints
dictionary:
document.getElementById("startButton").addEventListener("click", function() { navigator.mediaDevices.getUserMedia({ audio: { sampleSize: 8, echoCancellation: true } }).then(stream => audioElement.srcObject = stream) .catch(err => log(err.name + ": " + err.message)); }, false);
Here we see an event handler for a click
event which calls getUserMedia()
, specifying a set of audio constraints requesting that echo cancellation be enabled and that, if possible, the sample rate be 8 bits per sample instead of the more common 16 bits (possibly as a bandwidth saving measure). As long as an audio input device is available and the user allows it to be used, an audio track will be included in the resulting stream, and it will match the specified constraints as well as possible.
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'MediaStreamConstraints.audio' in that specification. | Editor's Draft | Initial specification. |
Feature | Chrome | Firefox (Gecko) | Microsoft Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | 38 (38) | ? | No support | (Yes) | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | 38.0 (38) | No support | ? | ? | (Yes) |
MediaDevices.getUserMedia()
MediaDevices.getSupportedConstraints()
MediaTrackSupportedConstraints
MediaTrackConstraints
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamConstraints/audio