Spaces:
Running
Running
Update mic_component.js
Browse files- mic_component.js +33 -40
mic_component.js
CHANGED
@@ -1,41 +1,34 @@
|
|
1 |
-
const
|
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 |
-
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
|
32 |
-
|
33 |
-
const handleAction = async () => {
|
34 |
-
const recorder = await recordAudio();
|
35 |
-
recorder.start();
|
36 |
-
await sleep(3000);
|
37 |
-
const audio = await recorder.stop();
|
38 |
-
audio.play();
|
39 |
-
};
|
40 |
-
|
41 |
-
document.querySelector("button").addEventListener("click", handleAction);
|
|
|
1 |
+
const recordButton = document.getElementById("recordButton");
|
2 |
+
const stopButton = document.getElementById("stopButton");
|
3 |
+
const audioChunks = [];
|
4 |
+
|
5 |
+
let mediaRecorder;
|
6 |
+
|
7 |
+
navigator.mediaDevices.getUserMedia({ audio: true })
|
8 |
+
.then(function(stream) {
|
9 |
+
mediaRecorder = new MediaRecorder(stream);
|
10 |
+
|
11 |
+
recordButton.onclick = function() {
|
12 |
+
mediaRecorder.start();
|
13 |
+
console.log("Recording started...");
|
14 |
+
};
|
15 |
+
|
16 |
+
stopButton.onclick = function() {
|
17 |
+
mediaRecorder.stop();
|
18 |
+
console.log("Recording stopped...");
|
19 |
+
};
|
20 |
+
|
21 |
+
mediaRecorder.ondataavailable = function(e) {
|
22 |
+
audioChunks.push(e.data);
|
23 |
+
};
|
24 |
+
|
25 |
+
mediaRecorder.onstop = function(e) {
|
26 |
+
const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
|
27 |
+
const reader = new FileReader();
|
28 |
+
reader.readAsDataURL(audioBlob);
|
29 |
+
reader.onloadend = function() {
|
30 |
+
const base64data = reader.result.split(',')[1];
|
31 |
+
Streamlit.setComponentValue(base64data);
|
32 |
};
|
33 |
+
};
|
34 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|