getConstraints() 方法在 MediaStreamTrack interface returns a MediaTrackConstraints object containing the set of constraints most recently established for the track using a prior call to applyConstraints() . These constraints indicate values and ranges of values that the Web site or application has specified are required or acceptable for the included constrainable properties.

Constraints can be used to ensure that the media meets certain guidelines you prefer. For example, you may prefer high definition 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 Capabilities, constraints, and settings for details on how to work with constrainable properties.

句法

const constraints = track.getConstraints()
					

返回值

A MediaTrackConstraints object which indicates the constrainable properties the Web site or app most recently set using applyConstraints() . The properties in the returned object are listed in the same order as when they were set, and only properties specifically set by the site or app are included.

注意: The returned set of constraints doesn't necessarily describe the actual state of the media. Even if any of the constraints couldn't be met, they are still included in the returned object as originally set by the site's code. To get the currently active settings for all constrainable properties, you should instead call getSettings() .

范例

This example obtains the current constraints for a track, sets the facingMode , and applies the updated constraints.

function switchCameras(track, camera) {
  const constraints = track.getConstraints();
  constraints.facingMode = camera;
  track.applyConstraints(constraints);
}
					

规范

规范 状态 注释
媒体捕获和流
The definition of 'getConstraints()' 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
getConstraints Chrome 53 Edge 12 Firefox 50 IE No Opera Yes Safari Yes WebView Android 53 Chrome Android 52 Firefox Android 50 Opera Android Yes Safari iOS No Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

元数据

  • 最后修改: