获取音视频设备
enumerateDevices
基本格式
var ePromise = navigator.mediaDevices.enumerateDevices();
MediaDevicesInfo
属性:
deviceID 设备ID
label 设备的名字
kind 设备的种类
groupID 两个设备groupID相同,说明是同一个物理设备
Promise
用于处理异步操作的对象,表示一个异步操作的最终完成(或失败)及其结果值
先处理handle,然后成功则执行resolve,否则执行reject
例子
index.html
<html><head><title> WEBRTC get audio and viedo devices</title></head><body><script src="./js/client.js"></script></body>
</html>
client.js
'use strict'
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {console.log('enumerateDevices is not supported');
} else {navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
}
function gotDevices(deviceInfos) {deviceInfos.forEach(function(deviceInfos) {console.log(deviceInfos.kind + ": label= "+ deviceInfos.label + ": id= "+ deviceInfos.deviceId + ": groupId= "+ deviceInfos.groupId);});
}
function handleError(err) {console.log(err.name + " : " + err.message);
}
实测没有使用https时,navigator.mediaDevices没有生效,直接打印enumerateDevices is not supported
,为了使用https,需要备案网站,而备案又会审核很久,所以最好尽快备案