获取设备
enumerateDevices(),方法可以获取的当前设备,所有可用设备。
代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<video id="video" controls autoplay></video>
</body>
</html>
<script>
// 获取所有可用设备
function updateDeviceList(){
window.navigator.mediaDevices.enumerateDevices()
.then(enumerateDevices => {
enumerateDevices.forEach(e =>{
console.log(`${e.deviceId} ${ e.kind} ${e.label} ${e.groupId}`)
})
})
.catch(err =>{
console.log(err.name + '' + err.message)
})
}
// addEventListener 监听变化
navigator.mediaDevices.addEventListener("devicechange", e =>{
console.log('devicechange 监听设备运行时候热拔插',)
updateDeviceList();
})
// 事件具柄监听变化
navigator.mediaDevices.ondevicechange = (event)=> {
console.log('ondevicechange句柄 监控设备运行时候热拔插')
updateDeviceList()
}
</script>