-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmuted_unmuted.html
132 lines (113 loc) · 3.83 KB
/
muted_unmuted.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html>
<head>
<meta name='description'
content='Simple MediaStreamTrack onmute/onunmute listener demo, based
originally on simpl.js demo from Sam Dutton.' />
<meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1'>
<meta http-equiv='X-UA-Compatible' content='chrome=1' />
<base target='_blank'>
<title>MediaStreamTrack onmute/onunmute</title>
<style>
div.select {
margin: 0 0 1em 0;
}
label {
color: white;
}
</style>
</head>
<body>
<div id='container'>
<h1>MediaStreamTrack onmute/onunmute etc.</h1>
<video muted autoplay width="320" height="240"></video>
<div class='select'>
<label for='videoSource'>Video source: </label>
<select id='videoSource'></select>
</div>
<br>
<textarea id="textarea" name="textarea" rows="10" cols="60">
</textarea>
<button style="background: #064;" onclick="toggleEnable();">
MediaStreamTrack onmute/onunmute</button>
</div>
<script>
var videoElement = document.querySelector("video");
var videoSelect = document.querySelector("select#videoSource");
var startButton = document.querySelector("button#start");
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
function gotSources(sourceInfos) {
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
var option = document.createElement("option");
option.value = sourceInfo.id;
if (sourceInfo.kind === 'video') {
option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Some other kind of source: ', sourceInfo);
}
}
}
function successCallback(stream) {
window.stream = stream; // make stream available to console
videoElement.src = window.URL.createObjectURL(stream);
videoElement.play();
grabTracksAndDoStuff(stream);
}
function errorCallback(error){
console.log("navigator.getUserMedia error: ", error);
}
function start(){
if (!!window.stream) {
videoElement.src = null;
window.stream.stop();
}
var videoSource = videoSelect.value;
var constraints = {
video: {
optional: [{sourceId: videoSource}]
}
};
navigator.getUserMedia(constraints, successCallback, errorCallback);
}
function grabTracksAndDoStuff(stream) {
tracks = stream.getVideoTracks();
document.getElementById('textarea').value = 'Track running corresponds to ' +
tracks[0].label;
if (tracks[0].enabled)
document.getElementById('textarea').value += '\nTrack is enabled X-D';
else
document.getElementById('textarea').value += '\nTrack is NOT enabled :?';
if (tracks[0].muted)
document.getElementById('textarea').value += '\nTrack is muted :?';
else
document.getElementById('textarea').value += '\nTrack is NOT muted XD';
tracks[0].onended = onEndedHandler;
tracks[0].onmute = onMuteHandler;
tracks[0].onunmute = onUnmuteHandler;
}
function onEndedHandler(track) {
document.getElementById('textarea').value += '\nReceived track onended.';
}
function onMuteHandler(track) {
document.getElementById('textarea').value += '\nReceived track onmute';
}
function onUnmuteHandler(track) {
document.getElementById('textarea').value += '\nReceived track onunmute.';
}
function toggleEnable() {
window.stream.getVideoTracks()[0].enabled =
!window.stream.getVideoTracks()[0].enabled;
}
if (typeof MediaStreamTrack === 'undefined'){
alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
} else {
MediaStreamTrack.getSources(gotSources);
}
videoSelect.onchange = start;
start();
</script>
</body>
</html>