-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_4.html
74 lines (67 loc) · 2.36 KB
/
example_4.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<title>Example 4</title>
</head>
<body>
<a href="./index.html"> << back</a>
<div class="center-align">
<h1>Example 4 - <span>Version_8 </span></h1>
<p>getUserMedia() - getTracks()</p>
<audio controls autoplay></audio>
<br>
<button class="btn waves-effect waves-light" id="startBtn">Start</button>
<button class="btn waves-effect waves-light" id="stopBtn" disabled>Stop</button>
<br>
<br>
<button class="btn waves-effect waves-light js-code">Show Code</button>
</div>
<hr>
<pre style="font-family: GillSans, Calibri, Trebuchet, sans-serif;" class="hide"></pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script class="containerScript">
let startBtn = document.getElementById('startBtn');
let stopBtn = document.getElementById('stopBtn');
let codeBtn = document.querySelector('.js-code');
let audioElement = document.querySelector('audio');
let pre = document.querySelector('pre');
startBtn.addEventListener('click', () => {
/**
* ask permission of the user for use microphone or camera
*/
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(gotStreamMethod2)
.catch(logError);
});
let gotStreamMethod2 = (stream) => {
startBtn.setAttribute('disabled', true);
stopBtn.removeAttribute('disabled');
window.stream = stream;
/**
* make stream available to console
*/
audioElement.srcObject = stream;
track = stream.getTracks()[0];
stopBtn.addEventListener('click', () => {
startBtn.removeAttribute('disabled');
stopBtn.setAttribute('disabled', true);
track.stop();
});
}
let logError = (error) => {
alert(error);
console.log(error);
}
codeBtn.addEventListener('click', () => {
pre.classList.toggle('hide');
pre.innerHTML = document.querySelector('.containerScript').innerHTML;
});
</script>
</body>
</html>