MediaStreamConstraints
dictionary's
视频
property is used to indicate what kind of video 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 videoConstraints = true | false | MediaTrackConstraints;
值对于
视频
property can be specified as either of two types:
布尔
true
, a video track is included; if no video source is available or if permission is not given to use the video source, the call to
getUserMedia()
will fail. If
false
, no video track is included.
MediaTrackConstraints
A constraints dictionary detailing the preferable and/or required values or ranges of values for the track's constrainable properties. If you specify constraints, a video track meeting these constraints is required.
In this example, we provide a simple value of
true
为
视频
property. This tells
getUserMedia()
that we require a video track, but we don't care about any specifics beyond that.
<p>Click the start button below to begin the demonstration.</p> <div id="startButton" class="button"> 开始 </div> <video id="video" width="160" height="120" autoplay></video><br> <div id="log"></div>
body {
font: 14px "Open Sans", "Arial", sans-serif;
}
video {
margin-top: 20px;
border: 1px solid black;
}
.button {
cursor: pointer;
width: 160px;
border: 1px solid black;
font-size: 16px;
text-align: center;
padding-top: 2px;
padding-bottom: 4px;
color: white;
background-color: darkgreen;
}
let videoElement = document.getElementById("video");
let logElement = document.getElementById("log");
function log(msg) {
logElement.innerHTML += msg + "<br>";
}
document.getElementById("startButton").addEventListener("click", function() {
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => videoElement.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 a video-only stream with no specific constraints, then attaches the resulting stream to a
<video>
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:
<p>Click the start button below to begin the demonstration.</p> <div id="startButton" class="button"> 开始 </div> <video id="video" width="160" height="120" autoplay></video><br> <div id="log"></div>
body {
font: 14px "Open Sans", "Arial", sans-serif;
}
video {
margin-top: 20px;
border: 1px solid black;
}
.button {
cursor: pointer;
width: 160px;
border: 1px solid black;
font-size: 16px;
text-align: center;
padding-top: 2px;
padding-bottom: 4px;
color: white;
background-color: darkgreen;
}
let videoElement = document.getElementById("video");
let logElement = document.getElementById("log");
function log(msg) {
logElement.innerHTML += msg + "<br>";
}
document.getElementById("startButton").addEventListener("click", function() {
navigator.mediaDevices.getUserMedia({
video: {
width: 160,
height: 120,
frameRate: 15
}
}).then(stream => videoElement.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 video constraints that indicate a preference for a video track whose dimensions are as close as possible to 160x120 pixels, and whose frame rate is as close to 15 frames per second as possible. As long as a video input device is available and the user allows it to be used, a video track will be included in the resulting stream, and it will match the specified constraints as well as possible.
| 规范 | 状态 | 注释 |
|---|---|---|
|
媒体捕获和流
The definition of 'MediaStreamConstraints.video' in that specification. |
候选推荐 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
视频
|
Chrome Yes | Edge ≤79 | Firefox 38 | IE No | Opera Yes | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android 38 | Opera Android Yes | Safari iOS ? | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
MediaDevices.getUserMedia()
MediaDevices.getSupportedConstraints()
MediaTrackSupportedConstraints
MediaTrackConstraints
MediaStreamConstraints
AudioStreamTrack
BlobEvent
CanvasCaptureMediaStream
ConstrainBoolean
ConstrainDOMString
ConstrainDouble
ConstrainLong
DoubleRange
HTMLCanvasElement.captureStream()
LongRange
MediaDevices
MediaStream
MediaStreamTrack
MediaStreamTrackEvent
MediaTrackCapabilities
MediaTrackConstraints
MediaTrackSettings
MediaTrackSupportedConstraints
Navigator.mediaDevices
NavigatorUserMedia
NavigatorUserMediaError
VideoStreamTrack
navigator.mediaDevices.getUserMedia()