MediaDevices 方法 enumerateDevices() requests a list of the available media input and output devices, such as microphones, cameras, headsets, and so forth. 返回的 Promise is resolved with a MediaDeviceInfo array describing the devices.

句法

var enumeratorPromise = navigator.mediaDevices.enumerateDevices();
					

返回值

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.

If enumeration fails, the promise is rejected.

范例

此处范例,使用 enumerateDevices() . It simply 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=
					

规范

规范 状态 注释
媒体捕获和流
The definition of 'mediaDevices: enumerateDevices' in that specification.
候选推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
enumerateDevices Chrome 47 Edge 12 Firefox 63 Disabled
63 Disabled
Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
39
IE No Opera 34 Safari 11 WebView Android 47 Chrome Android 47 Firefox Android 63 Disabled
63 Disabled
Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
39
Opera Android 34 Safari iOS 11 Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: