通过浏览器自带的API实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 获取设备列表
async getDeviceList() {
return new Promise(resolve => {
navigator.mediaDevices.enumerateDevices().then(devices => {
let audioDevices = [];
let videoDevices = [];
for (let i = 0; i < devices.length; i++) {
if (devices[i].kind === 'audioinput') {
audioDevices.push(devices[i]);
} else if (devices[i].kind === 'videoinput') {
videoDevices.push(devices[i]);
}
}
resolve({ audio: audioDevices, video: videoDevices });
}).catch((err) => {
console.log(err);
resolve({ audio: [], video: [] });
});
});
}