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:

布尔
If a Boolean value is specified, it simply indicates whether or not a video track should be included in the returned stream; if it's 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.

范例

Using a Boolean value

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.

HTML 内容

<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>
					

CSS content

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;
}
					

JavaScript 内容

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.

结果

Using a MediaTrackConstraints object

Now let's look at a similar example that uses a set of constraints based on the MediaTrackConstraints dictionary:

HTML 内容

<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>
					

CSS content

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;
}
					

JavaScript 内容

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.
候选推荐 最初的规范。

浏览器兼容性

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
视频 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

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: