-
Notifications
You must be signed in to change notification settings - Fork 36
/
camera_new.html
46 lines (42 loc) · 1.26 KB
/
camera_new.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
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Camera with mediaDevice</title>
</head>
<body>
Camera with mediaDevice.getUserMedia() (Chromeはフラグ設定が必要です)<br />
<button onclick="startVideo()">Start</button>
<button onclick="stopVideo()">Stop</button>
<br />
<video id="local_video" autoplay style="width: 320px; height: 240px; border: 1px solid black;"></video>
</body>
<script type="text/javascript">
let localVideo = document.getElementById('local_video');
let localStream;
// start local video
function startVideo() {
navigator.mediaDevices.getUserMedia({video: true, audio: false})
.then(function (stream) { // success
localStream = stream;
localVideo.srcObject = stream
localVideo.onloadedmetadata = function (e) {
localVideo.play();
};
}).catch(function (error) { // error
console.error('mediaDevice.getUserMedia() error:', error);
return;
});
}
// stop local video
function stopVideo() {
for (track of localStream.getTracks()) {
track.stop();
}
localStream = null;
localVideo.pause();
window.URL.revokeObjectURL(localVideo.src);
localVideo.src = '';
}
</script>
</html>