-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenumdemo5.html
82 lines (72 loc) · 2 KB
/
enumdemo5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<title>enumeration demo</title>
</head>
<body>
Devices:
<ol id="devicelist"></ol>
<button id="refresh" onclick="refreshDeviceList();">Refresh device list</button><br>
<script>
var device_list = null;
window.onload = onLoad;
let i=0;
function onLoad() {
device_list = document.getElementById("devicelist");
navigator.mediaDevices.getUserMedia(
{audio:true, video:true}).then(
function(stream) {
stream.getAudioTracks()[0].stop();
stream.getVideoTracks()[0].stop();
console.log("stream closed");
refreshDeviceList();
},
function(err) {addItem(err + ' ' + err.name + ': ' + err.message);}
);
}
function addItem(strItem) {
var item = document.createElement("li");
item.innerHTML = strItem;
device_list.appendChild(item);
return item;
}
function clearItems() {
if (!device_list) {
device_list = document.getElementById("devicelist");
}
while (device_list.firstChild ){
device_list.removeChild(device_list.firstChild );
}
}
function refreshDeviceList() {
clearItems();
if (!navigator.mediaDevices) {
addItem('navigator.mediaDevices not supported');
return;
}
if (!navigator.mediaDevices.enumerateDevices) {
addItem('enumerateDevices not supported');
return;
}
navigator.mediaDevices.enumerateDevices().then( function(infos) {
clearItems();
for (let i = 0; i < infos.length; i++) {
let devItem = addItem(infos[i].kind +
' - ' + infos[i].deviceId +
' - ' + infos[i].label +
' - ' + infos[i].groupId);
if (infos[i].getCapabilities) {
let capList = document.createElement('ul');
let capItem = document.createElement('li');
capItem.innerHTML = '<pre>' +JSON.stringify(infos[i].getCapabilities(), null, 2) + '</pre>';
capList.appendChild(capItem);
devItem.appendChild(capList);
}
}
}, function(msg) {
alert('Something wrong: ' + msg);
});
}
</script>
</body>
</html>