Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +32 -5
static/script.js
CHANGED
@@ -3,13 +3,40 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
3 |
const audioPlayer = document.getElementById("audio-player");
|
4 |
const chatContainer = document.getElementById("chat-container");
|
5 |
|
|
|
|
|
|
|
6 |
recordBtn.addEventListener("click", function() {
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
const formData = new FormData();
|
12 |
-
formData.append("audio", audioBlob);
|
13 |
|
14 |
fetch("/transcribe", {
|
15 |
method: "POST",
|
@@ -49,7 +76,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
49 |
});
|
50 |
});
|
51 |
});
|
52 |
-
}
|
53 |
|
54 |
function displayUserMessage(text) {
|
55 |
const messageElement = document.createElement("div");
|
|
|
3 |
const audioPlayer = document.getElementById("audio-player");
|
4 |
const chatContainer = document.getElementById("chat-container");
|
5 |
|
6 |
+
let mediaRecorder;
|
7 |
+
let chunks = [];
|
8 |
+
|
9 |
recordBtn.addEventListener("click", function() {
|
10 |
+
if (mediaRecorder && mediaRecorder.state === "recording") {
|
11 |
+
mediaRecorder.stop();
|
12 |
+
recordBtn.textContent = "Bonyeza kitufe cha kurekodi na uliza swali lako";
|
13 |
+
} else {
|
14 |
+
navigator.mediaDevices.getUserMedia({ audio: true })
|
15 |
+
.then(function(stream) {
|
16 |
+
mediaRecorder = new MediaRecorder(stream);
|
17 |
+
chunks = [];
|
18 |
+
|
19 |
+
mediaRecorder.addEventListener("dataavailable", function(e) {
|
20 |
+
chunks.push(e.data);
|
21 |
+
});
|
22 |
+
|
23 |
+
mediaRecorder.addEventListener("stop", function() {
|
24 |
+
const audioBlob = new Blob(chunks, { type: "audio/wav" });
|
25 |
+
sendAudioToServer(audioBlob);
|
26 |
+
});
|
27 |
|
28 |
+
mediaRecorder.start();
|
29 |
+
recordBtn.textContent = "Acha kurekodi";
|
30 |
+
})
|
31 |
+
.catch(function(err) {
|
32 |
+
console.error("Error accessing microphone:", err);
|
33 |
+
});
|
34 |
+
}
|
35 |
+
});
|
36 |
+
|
37 |
+
function sendAudioToServer(audioBlob) {
|
38 |
const formData = new FormData();
|
39 |
+
formData.append("audio", audioBlob, "recording.wav");
|
40 |
|
41 |
fetch("/transcribe", {
|
42 |
method: "POST",
|
|
|
76 |
});
|
77 |
});
|
78 |
});
|
79 |
+
}
|
80 |
|
81 |
function displayUserMessage(text) {
|
82 |
const messageElement = document.createElement("div");
|