/
RecordRTCPromisesHandler.html
80 lines (64 loc) · 2.17 KB
/
RecordRTCPromisesHandler.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
<style>
html, body {
margin: 0!important;
padding: 0!important;
overflow: hidden!important;
width: 100%;
}
</style>
<title>Promises and RecordRTC</title>
<h1>Promises and RecordRTC</h1>
<br>
<button id="btn-start-recording">Start Recording</button>
<button id="btn-stop-recording" disabled>Stop Recording</button>
<hr>
<video controls autoplay></video>
<script src="https://cdn.webrtc-experiment.com/RecordRTC.js"></script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script>
var video = document.querySelector('video');
function captureCamera(callback) {
navigator.mediaDevices.getUserMedia({audio: true, video: true}).then(function(camera) {
callback(camera);
}).catch(function(error) {
console.error(error);
});
}
function stopRecordingCallback() {
var blob = recorder.getBlob();
video.src = URL.createObjectURL(blob);
video.play();
recorder.camera.stop();
}
var recorder; // globally accessible
document.getElementById('btn-start-recording').onclick = function() {
this.disabled = true;
captureCamera(function(camera) {
setSrcObject(camera, video);
video.play();
recorder = new RecordRTCPromisesHandler(camera, {
type: 'video'
});
recorder.startRecording().then(function() {
console.info('startRecording success');
}).catch(function(error) {
console.error('startRecording failure', error);
});
// var internal = recorder.recordRTC.getInternalRecorder();
// alert(internal instanceof MediaStreamRecorder);
// release camera on stopRecording
recorder.camera = camera;
document.getElementById('btn-stop-recording').disabled = false;
});
};
document.getElementById('btn-stop-recording').onclick = function() {
this.disabled = true;
recorder.stopRecording().then(function() {
console.info('stopRecording success');
stopRecordingCallback();
// recorder.getDataURL().then(function(dataURL) {}).catch(errorCB);
}).catch(function(error) {
console.error('stopRecording failure', error);
});
};
</script>