概览
mediaDevices 是 Navigator 对象的只读属性,一个单列对象,可以连接访问相机和麦克风,屏幕共享等媒体输入设备
方法
enumerateDevices
请求一个可用的媒体输入和输出设备列表,如麦克风、相机、耳机等。返回的 Promise完成状态中是一个带有 MediaDeviceInfo 的数组
let mediaDevices = navigator.mediaDevices
if(!mediaDevices || !mediaDevices.enumerateDevices) return console.erorr('浏览器不支持enumerateDevices API')
navigator.mediaDevices.enumerateDevices()
.then((devices)=>{
for (let device of devices){
console.log(device.kind + ': ' +device.lable + ' id = '+ device.deviceId );
}
})
.catch(err=>{
console.error(err)
})
/*
audioinput: undefined id = default
audioinput: undefined id = communications
audioinput: undefined id = ac67d348685a08c75e5017f9a449b3d85f08dcb774c88ab95de82bbf2c0fc820
videoinput: undefined id = e41039bcfbc84d926a0b73cdc1d8b1daf3d67d36c62588202191d918fb076426
audiooutput: undefined id = default
audiooutput: undefined id = communications
audiooutput: undefined id = 015d73652e57bffb21679b937675d32c4d4a43862aba3774aaf0b5f1e983151f
*/
兼容性
getSupportedConstraints
返回一个 MediaTrackSupportedConstraints 对象,其属性都是客户端所支持约束的属性,值为 Boolean 类型
let supportedConstraints = navigator.mediaDevices.getSupportedConstrain