MediaDevices
interface provides access to connected media input devices like cameras and microphones, as well as screen sharing. In essence, it lets you obtain access to any hardware source of media data.
继承来自其父级接口的特性,
EventTarget
.
devicechange
ondevicechange
特性。
Inherits methods from its parent interface,
EventTarget
.
enumerateDevices()
Obtains an array of information about the media input and output devices available on the system.
getSupportedConstraints()
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()
MediaStream
for sharing or recording purposes. Returns a promise that resolves to a
MediaStream
.
getUserMedia()
MediaStream
containing a video track and/or an audio track with the input.
'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.onremovetrack = 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.height.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);
}
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
媒体捕获和流
The definition of 'MediaDevices' in that specification. |
候选推荐 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
MediaDevices
|
Chrome 47 | Edge ≤18 | Firefox 33 | IE 不支持 No | Opera 30 | Safari 11 | WebView Android 47 | Chrome Android 47 | Firefox Android 36 | Opera Android 30 | Safari iOS 11 | Samsung Internet Android 5.0 |
devicechange
event
|
Chrome 57 | Edge 12 | Firefox 52 | IE 不支持 No | Opera 34 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android ? | Opera Android 43 | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
enumerateDevices
|
Chrome 47 | Edge 12 |
Firefox
63
注意事项
Disabled
|
IE 不支持 No | Opera 34 | Safari 11 | WebView Android 47 | Chrome Android 47 |
Firefox Android
63
注意事项
Disabled
|
Opera Android 34 | Safari iOS 11 | Samsung Internet Android 5.0 |
getDisplayMedia()
|
Chrome
72
|
Edge
79
|
Firefox
66
|
IE 不支持 No |
Opera
60
|
Safari 13 |
WebView Android
不支持
No
注意事项
|
Chrome Android 不支持 No |
Firefox Android
不支持
No
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
getSupportedConstraints
|
Chrome 53 | Edge 12 | Firefox 44 | IE 不支持 No | Opera 40 | Safari 11 | WebView Android 53 | Chrome Android 52 | Firefox Android 50 | Opera Android 41 | Safari iOS 11 | Samsung Internet Android 6.0 |
getUserMedia
|
Chrome
52
|
Edge 12 |
Firefox
36
注意事项
|
IE 不支持 No |
Opera
40
|
Safari 11 | WebView Android 53 |
Chrome Android
52
|
Firefox Android
36
注意事项
|
Opera Android
41
|
Safari iOS 11 | Samsung Internet Android 6.0 |
ondevicechange
|
Chrome 57 | Edge 12 |
Firefox
52
|
IE 不支持 No | Opera 34 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android Yes | Opera Android 34 | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
| Stereo audio capture | Chrome ? | Edge ? | Firefox 55 | IE 不支持 No | Opera ? | Safari 不支持 No | WebView Android ? | Chrome Android ? | Firefox Android 不支持 No | Opera Android ? | Safari iOS 不支持 No | Samsung Internet Android ? |
完整支持
不支持
兼容性未知
见实现注意事项。
用户必须明确启用此特征。
getDisplayMedia()
方法。
Navigator.mediaDevices
: Returns a reference to a
MediaDevices
object that can be used to access devices.
MediaDevices
and the MediaStream Recording API (
source on GitHub
)
MediaDevices
and the MediaStream Recording API for video recording (
source on GitHub
)
MediaDevices
AudioStreamTrack
BlobEvent
CanvasCaptureMediaStream
ConstrainBoolean
ConstrainDOMString
ConstrainDouble
ConstrainLong
DoubleRange
HTMLCanvasElement.captureStream()
LongRange
MediaStream
MediaStreamTrack
MediaStreamTrackEvent
MediaTrackCapabilities
MediaTrackConstraints
MediaTrackSettings
MediaTrackSupportedConstraints
Navigator.mediaDevices
NavigatorUserMedia
NavigatorUserMediaError
VideoStreamTrack
navigator.mediaDevices.getUserMedia()