-
Notifications
You must be signed in to change notification settings - Fork 3
/
Recording.js
111 lines (111 loc) · 5.22 KB
/
Recording.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(function (Scratch) {
'use strict';
if (!Scratch.extensions.unsandboxed) {
throw new Error('Recording must be run unsandboxed');
}
/** @type {MediaRecorder|null} */
let mediaRecorder = null;
let recordedChunks = [];
const icon = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNjg1MTAzNzAzNDU1IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjMzMjIiIGRhdGEtc3BtLWFuY2hvci1pZD0iYTMxM3guNzc4MTA2OS4wLmkxIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgd2lkdGg9IjIwMCIgaGVpZ2h0PSIyMDAiPjxwYXRoIGQ9Ik04MzMgNDQ1LjVjMTcuNjczIDAgMzIgMTQuMzI3IDMyIDMyIDAgMTgzLjcyMi0xNDAuNTUzIDMzNC42MTYtMzE5Ljk5NyAzNTEuMDIxTDU0NSA4OTVoMTk0YzE3LjY3MyAwIDMyIDE0LjMyNyAzMi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gMjQuNzg4LTM3LjQxM2wwLjUxOC0wLjEwNWM1NC4yMDktMTAuNTM3IDg5LjgtNjIuNjA0IDgwLjE3OC0xMTYuNzc0bC0wLjMwNS0xLjY0MmMtMy4zNzItMTcuMzQ4IDcuOTU4LTM0LjE0NiAyNS4zMDYtMzcuNTE4IDE3LjM0OS0zLjM3MiAzNC4xNDYgNy45NTggMzcuNTE4IDI1LjMwNnoiIGZpbGw9IiNmZmZmZmYiIHAtaWQ9IjMzMjMiPjwvcGF0aD48L3N2Zz4=';
class Recording {
getInfo() {
return {
id: 'sipcrecording',
name: 'Recording',
color1: '#696969',
blocks: [
{
opcode: 'startRecording',
blockType: Scratch.BlockType.COMMAND,
text: 'Start recording',
blockIconURI: icon,
arguments: {}
},
{
opcode: 'stopRecording',
blockType: Scratch.BlockType.COMMAND,
text: 'Stop recording',
blockIconURI: icon,
arguments: {}
},
{
opcode: 'stopRecordingAndDownload',
blockType: Scratch.BlockType.COMMAND,
text: 'Stop recording and download with [name] as filename',
blockIconURI: icon,
arguments: {
name: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'recording.wav'
}
}
},
{
opcode: 'isRecording',
blockType: Scratch.BlockType.BOOLEAN,
text: 'Recording?',
blockIconURI: icon,
arguments: {}
},
]
};
}
async startRecording() {
recordedChunks = [];
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
console.error('The recording function is not supported by the browser');
return;
}
try {
if (!await Scratch.canRecordAudio()) {
throw new Error('VM denied permission');
}
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.addEventListener('dataavailable', function (e) {
recordedChunks.push(e.data);
});
mediaRecorder.start();
console.log('Start recording');
} catch (e) {
console.error('Could not start recording', e);
}
}
stopRecording() {
if (!mediaRecorder) {
console.error('Recording not started');
return;
}
console.log('Stop recording');
mediaRecorder.stop();
mediaRecorder = null;
recordedChunks = [];
}
stopRecordingAndDownload({name}) {
if (!mediaRecorder) {
console.error('Recording not started');
return;
}
console.log('Stop recording');
mediaRecorder.addEventListener('stop', function () {
const blob = new Blob(recordedChunks, { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = name;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(url);
recordedChunks = [];
});
mediaRecorder.stop();
mediaRecorder = null;
}
isRecording() {
return !!mediaRecorder;
}
}
Scratch.extensions.register(new Recording());
})(Scratch);
//BY -SIPC- 502415953