--- title: MediaDevices slug: Web/API/MediaDevices tags: - API - Audio - Conference - Devices - Interface - Media - Media Capture and Streams API - Media Streams API - MediaDevices - Reference - Screen Capture - Screen Capture API - Sharing - Video - WebRTC - インターフェイス - メディアストリーム - 画面キャプチャ translation_of: Web/API/MediaDevices ---
MediaDevices インターフェイスは、カメラやマイク、さらに画面共有などの接続されたメディア入力デバイスへのアクセスを提供します。要するに、メディアデータのソースであるハードウェアにアクセスすることができるようになります。
親インターフェイスである {{domxref("EventTarget")}} のプロパティを継承しています。
親インターフェイスである {{domxref("EventTarget")}} のメソッドを継承しています。
MediaStream で解決する 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);
}
}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('Media Capture', '#mediadevices', 'MediaDevices')}} | {{Spec2('Media Capture')}} | 初回定義 |
{{Compat("api.MediaDevices")}}
MediaDevices への参照を返します。MediaDevices および MediaStream Recording API を使用しています (GitHub 上のソース)MediaDevices および MediaStream Recording API を録画に使用しています (GitHub 上のソース)。