-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cb83cc
commit 6a324cb
Showing
2 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
marvin.interaction.web/src/main/resources/static/audiorecorder.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!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>Audio Recorder</h1> | ||
<button id="startBtn">Start Recording</button> | ||
<button id="stopBtn" disabled>Stop Recording</button> | ||
<p id="status">Press "Start Recording" to begin.</p> | ||
|
||
<script> | ||
let mediaRecorder; | ||
let audioChunks = []; | ||
|
||
document.getElementById('startBtn').addEventListener('click', async () => { | ||
try { | ||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); | ||
mediaRecorder = new MediaRecorder(stream); | ||
audioChunks = []; | ||
|
||
mediaRecorder.ondataavailable = (event) => { | ||
if (event.data.size > 0) { | ||
audioChunks.push(event.data); | ||
} | ||
}; | ||
|
||
mediaRecorder.onstop = async () => { | ||
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); | ||
const arrayBuffer = await audioBlob.arrayBuffer(); | ||
const byteArray = new Uint8Array(arrayBuffer); | ||
|
||
document.getElementById('status').innerText = "Uploading audio..."; | ||
|
||
// Post the byte array to the server | ||
fetch('/speech', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/octet-stream' }, | ||
body: byteArray | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
document.getElementById('status').innerText = "Audio uploaded successfully!"; | ||
} else { | ||
document.getElementById('status').innerText = "Failed to upload audio."; | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error('Error uploading audio:', error); | ||
document.getElementById('status').innerText = "An error occurred."; | ||
}); | ||
}; | ||
|
||
mediaRecorder.start(); | ||
document.getElementById('status').innerText = "Recording..."; | ||
document.getElementById('startBtn').disabled = true; | ||
document.getElementById('stopBtn').disabled = false; | ||
} catch (error) { | ||
console.error('Error accessing microphone:', error); | ||
document.getElementById('status').innerText = "Error accessing microphone."; | ||
} | ||
}); | ||
|
||
document.getElementById('stopBtn').addEventListener('click', () => { | ||
if (mediaRecorder && mediaRecorder.state !== 'inactive') { | ||
mediaRecorder.stop(); | ||
document.getElementById('status').innerText = "Stopped recording."; | ||
document.getElementById('startBtn').disabled = false; | ||
document.getElementById('stopBtn').disabled = true; | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |