applyConstraints () 方法在 MediaStreamTrack interface applies a set of constraints to the track; these constraints let the Web site or app establish ideal values and acceptable ranges of values for the constrainable properties of the track, such as frame rate, dimensions, echo cancelation, and so forth.

Constraints can be used to ensure that the media meets certain guidelines you prefer. For example, you may prefer high-density video but require that the frame rate be a little low to help keep the data rate low enough not overtax the network. Constraints can also specify ideal and/or acceptable sizes or ranges of sizes. See Applying constraints in Capabilities, constraints, and settings for more information on how to apply your preferred constraints.

句法

const appliedPromise = track.applyConstraints([constraints])
					

参数

constraints 可选
MediaTrackConstraints object listing the constraints to apply to the track's constrainable properties; any existing constraints are replaced with the new values specified, and any constrainable properties not included are restored to their default constraints. If this parameter is omitted, all currently set custom constraints are cleared. This object represents the basic set of constraints that must apply for the Promise to resolve. The object may contain an advanced property containing an array of additional MediaTrackConstrants objects, which are treated as exact requires.

返回值

A Promise which resolves when the constraints have been successfully applied. If the constraints cannot be applied, the promise is rejected with a MediaStreamError whose name is OverconstrainedError , to indicate that the constraints could not be met. This can happen if the specified constraints are too strict to find a match when attempting to configure the track.

范例

The following shows how to specify a basic and advanced set of constraints. It specifies that the page or web app needs a width between 640 and 1280 and a height between 480 and 720, with the later number in each pair being preferred. The advanced property further specifies that an image size of 1920 by 1280 is the preferred or an aspect ratio of 1.333 if that is not available. Note that these constraints also illustrate what the spec refers to as a backoff strategy .

const constraints = {
  width: {min: 640, ideal: 1280},
  height: {min: 480, ideal: 720},
  advanced: [
    {width: 1920, height: 1280},
    {aspectRatio: 1.333}
  ]
};
navigator.mediaDevices.getUserMedia({ video: true })
.then(mediaStream => {
  const track = mediaStream.getVideoTracks()[0];
  track.applyConstraints(constraints)
  .then(() => {
    // Do something with the track such as using the Image Capture API.
  })
  .catch(e => {
    // The constraints could not be satisfied by the available devices.
  });
});
					

规范

规范 状态 注释
媒体捕获和流
The definition of 'applyConstraints()' in that specification.
候选推荐 初始定义。
MediaStream 图像捕获
The definition of 'applyConstraints()' in that specification.
工作草案 Adds image constraints.

浏览器兼容性

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
applyConstraints Chrome 63 Edge 12 Firefox 50 IE No Opera 46 Safari Yes WebView Android 63 Chrome Android 63 Firefox Android 50 Opera Android 43 Safari iOS Yes Samsung Internet Android 7.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: