MediaDevices.enumerateDevices()

MediaDevices 的方法 enumerateDevices() 请求一个可用的媒体输入和输出设备的列表,例如麦克风,摄像机,耳机设备等。 返回的 Promise 完成时,会带有一个描述设备的 MediaDeviceInfo 的数组。

语法

var enumeratorPromise = navigator.mediaDevices.enumerateDevices();

返回值

返回一个 Promise 。当完成时,它接收一个 MediaDeviceInfo 对象的数组。每个对象描述一个可用的媒体输入输出设备。.

如果枚举失败,promise 也会被拒绝(rejected)。

示例

这是一个使用 enumerateDevices() 的例子。它只是输出一个带有标签(有标签的情况下)的 device ID 的列表。

if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
  console.log("不支持 enumerateDevices() .");
  return;
}

// 列出相机和麦克风。

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);
});

这将会输出类似以下的内容:

videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

或者,如果有一个或多个 MediaStream 处于活动状态或者获得了持久授权,将会输出以下内容:

videoinput: FaceTime HD Camera (Built-in) id=csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: default (Built-in Microphone) id=RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: Built-in Microphone id=r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

规范

规范 状态 注释
Media Capture and Streams
mediaDevices: enumerateDevices
Candidate Recommendation Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
enumerateDevicesChrome Full support 47Edge Full support 12Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes 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.
Full support 39
IE No support NoOpera Full support 34Safari Full support 11WebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes 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.
Full support 39
Opera Android Full support 34Safari iOS Full support 11Samsung Internet Android Full support 5.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also