MediaDevices.enumerateDevices() - Web APIs 编辑
The MediaDevices
method enumerateDevices()
requests a list of the available media input and output devices, such as microphones, cameras, headsets, and so forth. The returned Promise
is resolved with a MediaDeviceInfo
array describing the devices.
Syntax
var enumeratorPromise = navigator.mediaDevices.enumerateDevices();
Return value
A Promise
that receives an array of MediaDeviceInfo
objects when the promise is fulfilled. Each object in the array describes one of the available media input and output devices. The order is significant - the default capture devices will be listed first.
If enumeration fails, the promise is rejected.
Example
Here's an example of using enumerateDevices()
. It outputs a list of the device IDs, with their labels if available.
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
console.log("enumerateDevices() not supported.");
return;
}
// List cameras and microphones.
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
});
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
This might produce:
videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8= audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM= audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=
or if one or more MediaStream
s are active or persistent permissions are granted:
videoinput: FaceTime HD Camera (Built-in) id=csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8= audioinput: default (Built-in Microphone) id=RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM= audioinput: Built-in Microphone id=r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=
Specifications
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'mediaDevices: enumerateDevices' in that specification. | Candidate Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
navigator.mediaDevices.getUserMedia()
- WebRTC - the introductory page to the API
- MediaStream API - the API for the media stream objects
- Taking webcam photos - a tutorial on using
getUserMedia()
for taking photos rather than video.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论