MediaStreamTrack.applyConstraints()

这篇翻译不完整。请帮忙从英语翻译这篇文章

MediaStreamTrack接口的applyConstraints方法应用一组约束;这些约束让Web站点或应用程序为轨道的可约束属性(如帧速率、尺寸、回波抵消等)建立理想值和可接受的值范围。

可以使用约束来确保媒体符合您喜欢的某些指导方针。例如,您可能更喜欢高密度的视频,但是要求帧速率稍微低一点,以帮助保持足够低的数据速率,而不会给网络带来过多的负担。约束还可以指定理想的和/或可接受的大小或大小范围。有关如何应用首选约束的更多信息,请参见在功能、约束和设置中应用约束。

Syntax

var appliedPromise = MediaStreamTrack.applyConstraints(constraints);

Parameters

constraints 可选
A 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. 

Return value

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.

Examples

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.
  })
})

Specifications

Specification Status Comment
Media Capture and Streams
applyConstraints()
Candidate Recommendation Initial definition.
MediaStream Image Capture
applyConstraints()
Working Draft Adds image constraints.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
applyConstraintsChrome Full support 63Edge Full support 12Firefox Full support 50IE No support NoOpera Full support 46Safari Full support YesWebView Android Full support 63Chrome Android Full support 63Edge Mobile ? Firefox Android Full support 50Opera Android Full support 43Safari iOS Full support YesSamsung Internet Android Full support 7.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also

文档标签和贡献者

此页面的贡献者: mdnwebdocs-bot, rocky-ru
最后编辑者: mdnwebdocs-bot,