--- title: MediaDevices slug: Web/API/MediaDevices tags: - API - Experimental - Interface - Media - MediaDevices - NeedsTranslation - TopicStub - WebRTC - 媒体 - 媒体流 - 媒体设备 translation_of: Web/API/MediaDevices ---
MediaDevices
接口提供访问连接媒体输入的设备,如照相机和麦克风,以及屏幕共享等。它可以使你取得任何硬件资源的媒体数据。
从父类{{domxref("EventTarget")}}中继承的属性.
从其父项继承方法 {{domxref("EventTarget")}}.
'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); } }
规范 | 状态 | 评论 |
---|---|---|
{{SpecName('Media Capture', '#mediadevices', 'MediaDevices')}} | {{Spec2('Media Capture')}} | 初始定义 |
{{Compat("api.MediaDevices")}}
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)