这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
The MediaDevices interface provides access to connected media input devices like cameras and microphones, as well as screensharing.
Properties
Inherits properties from its parent EventTarget.
Event handlers
MediaDevices.ondevicechange- Returns the event handler for the
devicechangeevent type.
Methods
Inherits methods from its parent EventTarget.
EventTarget.addEventListener()- Registers an event handler to a specific event type.
MediaDevices.enumerateDevices()- Obtains an array of information about the media input and output devices available on the system.
MediaDevices.getUserMedia()- With the user's permission through a prompt, turns on a camera or screensharing and/or a microphone on the system and provides a
MediaStreamcontaining a video track and/or an audio track with the input. EventTarget.removeEventListener()- Removes an event listener.
Example
'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);
}
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| Media Capture and Streams MediaDevices |
Editor's Draft | Initial definition |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | 36.0 (36.0) | 未实现 | 未实现 | 未实现 |
enumerateDevices() |
51.0 | ? | 未实现 | 未实现 | 未实现 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | 未实现 | 未实现 | 36.0 (36.0) | 2.2 | 未实现 | 未实现 | 未实现 | 未实现 |
enumerateDevices() |
未实现 | 未实现 | ? | ? | 未实现 | 未实现 | 未实现 | 未实现 |
See also
- WebRTC - the introductory page to the API
- Navigator.getUserMedia()