|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Audio Recorder</title> |
|
</head> |
|
<body> |
|
<h1>Record and Send Audio</h1> |
|
<button id="start-recording">Start Recording</button> |
|
<button id="stop-recording" disabled>Stop Recording</button> |
|
<button id="send-audio" disabled>Send Audio</button> |
|
<audio id="audio-playback" controls></audio> |
|
<script> |
|
let mediaRecorder; |
|
let audioChunks = []; |
|
|
|
document.getElementById('start-recording').addEventListener('click', async () => { |
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
|
mediaRecorder = new MediaRecorder(stream); |
|
mediaRecorder.ondataavailable = event => { |
|
audioChunks.push(event.data); |
|
}; |
|
mediaRecorder.onstop = () => { |
|
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); |
|
const audioUrl = URL.createObjectURL(audioBlob); |
|
document.getElementById('audio-playback').src = audioUrl; |
|
document.getElementById('send-audio').disabled = false; |
|
}; |
|
mediaRecorder.start(); |
|
document.getElementById('start-recording').disabled = true; |
|
document.getElementById('stop-recording').disabled = false; |
|
}); |
|
|
|
document.getElementById('stop-recording').addEventListener('click', () => { |
|
mediaRecorder.stop(); |
|
document.getElementById('start-recording').disabled = false; |
|
document.getElementById('stop-recording').disabled = true; |
|
}); |
|
|
|
document.getElementById('send-audio').addEventListener('click', () => { |
|
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); |
|
const formData = new FormData(); |
|
formData.append('audio', audioBlob, 'audio.wav'); |
|
|
|
fetch('/upload-audio', { |
|
method: 'POST', |
|
body: formData |
|
}).then(response => response.json()) |
|
.then(data => { |
|
alert('Audio processed successfully'); |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
|