Created
July 3, 2026 08:37
-
-
Save marcominerva/4dc22e9c6379d459127abc310d93eb3d to your computer and use it in GitHub Desktop.
audio-recorder.js
This file contains hidden or 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
| let mediaStream; | |
| let mediaRecorder; | |
| let chunks = []; | |
| let isStopping = false; | |
| let recordingMimeType = "audio/webm"; | |
| export async function startRecording() { | |
| if (mediaRecorder?.state === "recording" || isStopping) { | |
| return; | |
| } | |
| if (!navigator.mediaDevices?.getUserMedia) { | |
| throw new Error("The browser does not support audio recording."); | |
| } | |
| try { | |
| mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true }); | |
| } catch (error) { | |
| throw new Error(getRecordingErrorMessage(error)); | |
| } | |
| chunks = []; | |
| mediaRecorder = new MediaRecorder(mediaStream); | |
| recordingMimeType = mediaRecorder.mimeType || "audio/webm"; | |
| mediaRecorder.addEventListener("dataavailable", onDataAvailable); | |
| mediaRecorder.start(); | |
| } | |
| export async function cancelRecording() { | |
| if (!mediaRecorder || isStopping) { | |
| return; | |
| } | |
| try { | |
| isStopping = true; | |
| await stopRecorder(); | |
| } finally { | |
| isStopping = false; | |
| releaseMediaStream(); | |
| } | |
| } | |
| export async function confirmRecording() { | |
| if (!mediaRecorder || isStopping) { | |
| releaseMediaStream(); | |
| throw new Error("No audio was captured."); | |
| } | |
| try { | |
| isStopping = true; | |
| const blob = await stopRecorder(); | |
| return { | |
| stream: DotNet.createJSStreamReference(blob), | |
| mimeType: blob.type || recordingMimeType || "audio/webm" | |
| }; | |
| } finally { | |
| isStopping = false; | |
| releaseMediaStream(); | |
| } | |
| } | |
| function onDataAvailable(event) { | |
| if (event.data.size > 0) { | |
| chunks.push(event.data); | |
| } | |
| } | |
| async function stopRecorder() { | |
| if (!mediaRecorder || mediaRecorder.state === "inactive") { | |
| const blob = new Blob(chunks, { type: recordingMimeType }); | |
| mediaRecorder = null; | |
| chunks = []; | |
| if (blob.size === 0) { | |
| throw new Error("No audio was captured."); | |
| } | |
| recordingMimeType = blob.type || recordingMimeType; | |
| return blob; | |
| } | |
| const recorder = mediaRecorder; | |
| const stopped = new Promise(resolve => { | |
| recorder.addEventListener("stop", () => { | |
| const mimeType = recorder.mimeType || recordingMimeType || "audio/webm"; | |
| resolve(new Blob(chunks, { type: mimeType })); | |
| }, { once: true }); | |
| }); | |
| recorder.stop(); | |
| const blob = await stopped; | |
| recorder.removeEventListener("dataavailable", onDataAvailable); | |
| mediaRecorder = null; | |
| chunks = []; | |
| if (blob.size === 0) { | |
| throw new Error("No audio was captured."); | |
| } | |
| return blob; | |
| } | |
| function releaseMediaStream() { | |
| if (mediaStream) { | |
| mediaStream.getTracks().forEach(track => track.stop()); | |
| mediaStream = null; | |
| } | |
| } | |
| function getRecordingErrorMessage(error) { | |
| if (error?.name === "NotAllowedError" || error?.name === "SecurityError") { | |
| return "Microphone access was denied. Allow browser permissions and try again."; | |
| } | |
| if (error?.name === "NotFoundError") { | |
| return "No microphone is available on this device."; | |
| } | |
| return "Unable to start audio recording."; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment