-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-stream.html
79 lines (64 loc) · 1.61 KB
/
media-stream.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
<html>
<head>
<title>NEJS 2016 - Media Stream</title>
<style>
body {
align-items: center;
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
}
.video {
width: 500px;
margin-bottom: 40px;
}
.buttons {
display: flex;
}
button {
font-size: 30px;
margin: 0 10px;
}
</style>
</head>
<body>
<video data-webcam class="video"></video>
<div class="buttons">
<button data-stream-on>Stream on</button>
<button data-stream-off>Stream off</button>
</div>
<script>
// create web audio context
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// DOM NODES
const ui = {
streamOn: document.querySelector('[data-stream-on]'),
streamOff: document.querySelector('[data-stream-off]'),
webcam: document.querySelector('[data-webcam]'),
};
let mediaStream;
// get user microphone
navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
}).then(stream => {
// create media stream from mic input and connect to output
mediaStream = audioCtx.createMediaStreamSource(stream);
// hook up webcam video
ui.webcam.autoplay = true;
ui.webcam.src = window.URL.createObjectURL(stream);
// connect media stream to output
ui.streamOn.addEventListener('click', e => {
mediaStream.connect(audioCtx.destination);
});
// disconnect media stream from output
ui.streamOff.addEventListener('click', e => {
mediaStream.disconnect(audioCtx.destination);
});
console.log(mediaStream);
});
console.log(audioCtx);
</script>
</body>
</html>