--- title: MediaDevices slug: Web/API/MediaDevices tags: - API - Interface - Media - Media Capture and Streams API - Media Streams API - MediaDevices - Reference - Screen Capture API - WebRTC translation_of: Web/API/MediaDevices ---
MediaDevices 인터페이스는 카메라, 마이크, 공유 화면 등 현재 연결된 미디어 입력 장치로의 접근 방법을 제공하는 인터페이스입니다. 다르게 말하자면, 미디어 데이터를 제공하는 모든 하드웨어로 접근할 수 있는 방법입니다.
부모 인터페이스인 {{domxref("EventTarget")}}의 속성을 상속합니다.
devicechangeondevicechange 속성으로도 사용할 수 있습니다.부모 인터페이스인 {{domxref("EventTarget")}}의 메서드를 상속합니다.
MediaStream으로 이행하는 {{jsxref("Promise")}}를 반환합니다.'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.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);
}
}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('Media Capture', '#mediadevices', 'MediaDevices')}} | {{Spec2('Media Capture')}} | Initial definition |
{{Compat("api.MediaDevices")}}
MediaDevices 객체의 참조를 반환하는 {{domxref("Navigator.mediaDevices")}}