-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecording_streamer.ts
More file actions
42 lines (38 loc) · 1.02 KB
/
recording_streamer.ts
File metadata and controls
42 lines (38 loc) · 1.02 KB
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
import { Streamer } from "./streamer";
export class RecordingStreamer extends Streamer {
recorder: MediaRecorder;
chunks: Blob[];
constructor(stream: MediaStream) {
super();
this.recorder = new MediaRecorder(stream);
this.chunks = [];
this.recorder.ondataavailable = (e) => {
this.chunks.push(e.data);
};
this.recorder.onstart = () => {
this.onStateChange("CONNECTED");
};
this.recorder.onstop = () => {
this.onStateChange("NOT_CONNECTED");
const finalData = new Blob(this.chunks, {
type: "audio/ogg; codecs=opus",
});
const url = URL.createObjectURL(finalData);
const a = document.createElement("a");
a.href = url;
a.download = "recorded.ogg";
a.click();
};
this.recorder.onerror = (e) => {
console.error(e);
this.onStateChange("CONNECTION_LOST");
};
}
async start(): Promise<void> {
this.chunks = [];
this.recorder.start();
}
async stop(): Promise<void> {
this.recorder.stop();
}
}