MediaDevices

翻译不完整。 请帮助我们翻译这篇文章!

MediaDevices 接口提供访问连接媒体输入的设备,如照相机和麦克风,以及屏幕共享等。In essence, it lets you obtain access to any hardware source of media data.

属性

从父类EventTarget中继承的属性.

事件

devicechange
返回 devicechange 事件类型的事件处理程序。
也可通过 ondevicechange 访问

方法

从其父项继承方法 EventTarget.

MediaDevices.enumerateDevices()
获取有关系统中可用的媒体输入和输出设备的一系列信息。
getSupportedConstraints()
Returns an object conforming to MediaTrackSupportedConstraints indicating which constrainable properties are supported on the MediaStreamTrack interface. See Capabilities and constraints in Media Capture and Streams API (Media Stream) to learn more about constraints and how to use them.
getDisplayMedia()
Prompts the user to select a display or portion of a display (such as a window) to capture as a MediaStream for sharing or recording purposes. Returns a promise that resolves to a MediaStream.
MediaDevices.getUserMedia()
在用户通过提示允许的情况下,打开系统上的相机或屏幕共享和/或麦克风,并提供 MediaStream 包含视频轨道和/或音频轨道的输入。

示例

'use strict';

// Put variables in global scope to make them available to the browser console.
var video = document.querySelector('video');
var constraints = window.constraints = {
  audio: false,
  video: true
};
var errorElement = document.querySelector('#errorMsg');

navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
  var videoTracks = stream.getVideoTracks();
  console.log('Got stream with constraints:', constraints);
  console.log('Using video device: ' + videoTracks[0].label);
  stream.onended = function() {
    console.log('Stream ended');
  };
  window.stream = stream; // make variable available to browser console
  video.srcObject = stream;
})
.catch(function(error) {
  if (error.name === 'ConstraintNotSatisfiedError') {
    errorMsg('The resolution ' + constraints.video.width.exact + 'x' +
        constraints.video.width.exact + ' px is not supported by your device.');
  } else if (error.name === 'PermissionDeniedError') {
    errorMsg('Permissions have not been granted to use your camera and ' +
      'microphone, you need to allow the page access to your devices in ' +
      'order for the demo to work.');
  }
  errorMsg('getUserMedia error: ' + error.name, error);
});

function errorMsg(msg, error) {
  errorElement.innerHTML += '<p>' + msg + '</p>';
  if (typeof error !== 'undefined') {
    console.error(error);
  }
}

规范

规范 状态 评论
Media Capture and Streams
MediaDevices
Candidate Recommendation 初始定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
MediaDevicesChrome Full support 47Edge Full support ≤18Firefox Full support 33IE No support NoOpera Full support 30Safari Full support 11WebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 36Opera Android Full support 30Safari iOS Full support 11Samsung Internet Android Full support 5.0
devicechange eventChrome Full support 57Edge Full support 12Firefox Full support 52IE No support NoOpera Full support 34Safari No support NoWebView Android No support NoChrome Android No support NoFirefox Android ? Opera Android Full support 43Safari iOS No support NoSamsung Internet Android No support No
enumerateDevicesChrome Full support 47Edge Full support 12Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 39
IE No support NoOpera Full support 34Safari Full support 11WebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 39
Opera Android Full support 34Safari iOS Full support 11Samsung Internet Android Full support 5.0
getDisplayMedia()Chrome Full support 72
Full support 72
No support 70 — 72
Notes Disabled
Notes Available as a member of Navigator instead of MediaDevices in Chrome 70 and 71.
Disabled From version 70 until version 72 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 79
Full support 79
Full support 17
Notes
Notes Available as a member of Navigator instead of MediaDevices.
Firefox Full support 66
Full support 66
No support 33 — 66
Notes
Notes Since Firefox 33 you can capture screen data using getUserMedia(), with a video constraint called mediaSource. Prior to 52 it relied on a client-configurable whitelist.
IE No support NoOpera Full support 60
Full support 60
No support 57 — 60
Notes Disabled
Notes Available as a member of Navigator instead of MediaDevices in Opera 57 and 58.
Disabled From version 57 until version 60 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled).
Safari Full support 13WebView Android No support No
Notes
No support No
Notes
Notes API is available, but will always fail with NotAllowedError.
Chrome Android No support NoFirefox Android No support No
Notes
No support No
Notes
Notes API is available, but will always fail with NotAllowedError.
Opera Android No support NoSafari iOS Full support 13Samsung Internet Android No support No
getSupportedConstraintsChrome Full support 53Edge Full support 12Firefox Full support 44IE No support NoOpera Full support 40Safari Full support 11WebView Android Full support 53Chrome Android Full support 52Firefox Android Full support 50Opera Android Full support 41Safari iOS Full support 11Samsung Internet Android Full support 6.0
getUserMediaChrome Full support 52
Full support 52
No support 47 — 52
Notes Disabled
Notes Older versions of Chrome implement navigator.webkitGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Disabled From version 47 until version 52 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 12Firefox Full support 36
Notes
Full support 36
Notes
Notes Older versions of Firefox implement navigator.mozGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Notes Before Firefox 55, getUserMedia() incorrectly returns NotSupportedError when the list of constraints specified is empty, or has all constraints set to false. Starting in Firefox 55, this situation now correctly calls the failure handler with a TypeError.
Notes When using the Firefox-specific video constraint called mediaSource to request display capture, Firefox 66 and later consider values of screen and window to both cause a list of screens and windows to be shown.
Notes Starting in Firefox 66, getUserMedia() can no longer be used in sandboxed <iframe>s or data URLs entered in the address bar by the user.
IE No support NoOpera Full support 40
Full support 40
No support 34 — 40
Notes Disabled
Notes Older versions of Opera implement navigator.webkitGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Disabled From version 34 until version 40 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled).
Safari Full support 11WebView Android Full support 53Chrome Android Full support 52
Full support 52
No support 47 — 52
Notes Disabled
Notes Older versions of Chrome implement navigator.webkitGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Disabled From version 47 until version 52 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 36
Notes
Full support 36
Notes
Notes Older versions of Firefox implement navigator.mozGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Notes Before Firefox 55, getUserMedia() incorrectly returns NotSupportedError when the list of constraints specified is empty, or has all constraints set to false. Starting in Firefox 55, this situation now correctly calls the failure handler with a TypeError.
Notes When using the Firefox-specific video constraint called mediaSource to request display capture, Firefox 66 and later consider values of screen and window to both cause a list of screens and windows to be shown.
Notes Starting in Firefox 66, getUserMedia() can no longer be used in sandboxed <iframe>s or data URLs entered in the address bar by the user.
Opera Android Full support 41
Full support 41
No support 34 — 41
Notes Disabled
Notes Older versions of Opera implement navigator.webkitGetUserMedia, a prefixed form of the legacy navigator.getUserMedia API.
Disabled From version 34 until version 41 (exclusive): this feature is behind the Experimental Web Platform features preference (needs to be set to Enabled).
Safari iOS Full support 11Samsung Internet Android Full support 6.0
ondevicechangeChrome Full support 57Edge Full support 12Firefox Full support 52
Full support 52
No support 51 — 52
Notes Disabled
Notes MediaDevices.ondevicechange is supported only on macOS.
Disabled From version 51 until version 52 (exclusive): this feature is behind the media.ondevicechange.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 34Safari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support YesOpera Android Full support 34Safari iOS No support NoSamsung Internet Android No support No
Stereo audio captureChrome ? Edge ? Firefox Full support 55IE No support NoOpera ? Safari No support NoWebView Android ? Chrome Android ? Firefox Android No support NoOpera Android ? Safari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

参见