-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RecordRTCPromisesHandler.html
71 lines (55 loc) · 1.99 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
<style>
html, body {
margin: 0!important;
padding: 0!important;
}
</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 playsinline></video>
<script src="/RecordRTC.js"></script>
<script>
const video = document.querySelector('video');
async function stopRecordingCallback() {
video.srcObject = null;
let blob = await recorder.getBlob();
video.src = URL.createObjectURL(blob);
recorder.stream.getTracks(t => t.stop());
// reset recorder's state
await recorder.reset();
// clear the memory
await recorder.destroy();
// so that we can record again
recorder = null;
}
let recorder; // globally accessible
document.getElementById('btn-start-recording').onclick = async function() {
this.disabled = true;
let stream = await navigator.mediaDevices.getUserMedia({video: true, audio: true});
video.srcObject = stream;
recorder = new RecordRTCPromisesHandler(stream, {
type: 'video'
});
await recorder.startRecording();
// helps releasing camera on stopRecording
recorder.stream = stream;
document.getElementById('btn-stop-recording').disabled = false;
// if you want to access internal recorder
const internalRecorder = await recorder.getInternalRecorder();
console.log('internal-recorder', internalRecorder.name);
// if you want to read recorder's state
console.log('recorder state: ', await recorder.getState());
};
document.getElementById('btn-stop-recording').onclick = async function() {
this.disabled = true;
await recorder.stopRecording();
stopRecordingCallback();
document.getElementById('btn-start-recording').disabled = false;
};
</script>
<footer style="margin-top: 20px;"><small id="send-message"></small></footer>
<script src="https://www.webrtc-experiment.com/common.js"></script>