Create audio_recorder.html
Browse files- audio_recorder.html +56 -0
audio_recorder.html
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Audio Recorder</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<h1>Record and Send Audio</h1>
|
| 10 |
+
<button id="start-recording">Start Recording</button>
|
| 11 |
+
<button id="stop-recording" disabled>Stop Recording</button>
|
| 12 |
+
<button id="send-audio" disabled>Send Audio</button>
|
| 13 |
+
<audio id="audio-playback" controls></audio>
|
| 14 |
+
<script>
|
| 15 |
+
let mediaRecorder;
|
| 16 |
+
let audioChunks = [];
|
| 17 |
+
|
| 18 |
+
document.getElementById('start-recording').addEventListener('click', async () => {
|
| 19 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 20 |
+
mediaRecorder = new MediaRecorder(stream);
|
| 21 |
+
mediaRecorder.ondataavailable = event => {
|
| 22 |
+
audioChunks.push(event.data);
|
| 23 |
+
};
|
| 24 |
+
mediaRecorder.onstop = () => {
|
| 25 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
| 26 |
+
const audioUrl = URL.createObjectURL(audioBlob);
|
| 27 |
+
document.getElementById('audio-playback').src = audioUrl;
|
| 28 |
+
document.getElementById('send-audio').disabled = false;
|
| 29 |
+
};
|
| 30 |
+
mediaRecorder.start();
|
| 31 |
+
document.getElementById('start-recording').disabled = true;
|
| 32 |
+
document.getElementById('stop-recording').disabled = false;
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
document.getElementById('stop-recording').addEventListener('click', () => {
|
| 36 |
+
mediaRecorder.stop();
|
| 37 |
+
document.getElementById('start-recording').disabled = false;
|
| 38 |
+
document.getElementById('stop-recording').disabled = true;
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
document.getElementById('send-audio').addEventListener('click', () => {
|
| 42 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
| 43 |
+
const formData = new FormData();
|
| 44 |
+
formData.append('audio', audioBlob, 'audio.wav');
|
| 45 |
+
|
| 46 |
+
fetch('/upload-audio', {
|
| 47 |
+
method: 'POST',
|
| 48 |
+
body: formData
|
| 49 |
+
}).then(response => response.json())
|
| 50 |
+
.then(data => {
|
| 51 |
+
alert('Audio processed successfully');
|
| 52 |
+
});
|
| 53 |
+
});
|
| 54 |
+
</script>
|
| 55 |
+
</body>
|
| 56 |
+
</html>
|