MediaDevices 接口提供访问连接媒体输入的设备,如照相机和麦克风,以及屏幕共享等。它可以使你取得任何硬件资源的媒体数据。
属性从父类EventTarget中继承的属性.
事件devicechange返回 devicechange 事件类型的事件处理程序。 也可通过 ondevicechange 访问方法从其父项继承方法 EventTarget.
MediaDevices.enumerateDevices()获取有关系统中可用的媒体输入和输出设备的一系列信息。getSupportedConstraints()返回一个符合 MediaTrackSupportedConstraints (en-US) 的对象。该对象指明了 MediaStreamTrack 接口支持的可约束的属性。查看 Capabilities and constraints in Media Capture and Streams API (Media Stream) 去了解更多相关信息。getDisplayMedia()提示用户选择显示器或显示器的一部分(例如窗口)以捕获为MediaStream 以便共享或记录。返回解析为MediaStream的Promise。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 consolevideo.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 | 初始定义 |
desktop | mobile | |||||||||||
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox Android | Opera Android | iOS Safari | Samsung Internet | |
MediaDevices | Full support47 | Full support12 | Full support33 | No supportNo | Full support30 | Full support11 | Full support47 | Full support47 | Full support36 | Full support30 | Full support11 | Full support5.0 |
devicechange event | Full support57 | Full support12 | Full support52 | No supportNo | Full support34 | Full support11 | No supportNo | No supportNo | Compatibility unknown; please update this.? | Full support43 | Full support11 | No supportNo |
enumerateDevices | Full support47 | Full support12 | Full support39Open | No supportNo | Full support34 | Full support11 | Full support47 | Full support47 | Full support39Open | Full support34 | Full support11 | Full support5.0 |
getDisplayMedia() | Full support72Open | Full support79Open | Full support66Open | No supportNo | Full support60Open | Full support13 | No supportNofootnoteOpen | No supportNo | No supportNofootnoteOpen | No supportNo | No supportNo | No supportNo |
Audio capture support | Full support74footnoteOpen | Full support≤ 79footnoteOpen | No supportNo | No supportNo | Compatibility unknown; please update this.? | No supportNo | No supportNo | No supportNo | No supportNo | No supportNo | No supportNo | No supportNo |
getSupportedConstraints | Full support53 | Full support12 | Full support44 | No supportNo | Full support40 | Full support11 | Full support53 | Full support52 | Full support50 | Full support41 | Full support11 | Full support6.0 |
getUserMedia | Full support53footnoteOpen | Full support12 | Full support36footnoteOpen | No supportNo | Full support40footnoteOpen | Full support11 | Full support53 | Full support53footnoteOpen | Full support36footnoteOpen | Full support41footnoteOpen | Full support11 | Full support6.0 |
Secure context required | Full support53 | Full support79 | Full support68 | No supportNo | Full support40 | Compatibility unknown; please update this.? | Full support53 | Full support53 | Full support68 | Full support41 | Compatibility unknown; please update this.? | Full support6.0 |
ondevicechange | Full support57 | Full support12 | Full support52Open | No supportNo | Full support34 | Full support11 | No supportNo | No supportNo | Full supportYes | Full support34 | Full support11 | No supportNo |
Stereo audio capture | Compatibility unknown; please update this.? | Compatibility unknown; please update this.? | Full support55 | No supportNo | Compatibility unknown; please update this.? | No supportNo | Compatibility unknown; please update this.? | Compatibility unknown; please update this.? | No supportNo | Compatibility unknown; please update this.? | No supportNo | Compatibility unknown; please update this.? |