From 4e1e7eba0ceb06b692f48f14810411af919e9b32 Mon Sep 17 00:00:00 2001 From: Jason Praful Date: Sat, 9 Aug 2025 13:19:43 +0100 Subject: [PATCH] allow passing configuration for mediaRecorder --- README.md | 1 + src/hooks/useVoiceVisualizer.tsx | 8 +++++++- src/types/types.ts | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ec764b8..9d87c6e 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ const recorderControls = useVoiceVisualizer(); | `onResumedAudioPlayback` | `() => void` | Callback function triggered when audio playback is resumed. | | `onErrorPlayingAudio` | `(error: Error) => void` | Callback function is invoked when an error occurs during the execution of `audio.play()`. It provides an opportunity to handle and respond to such error. | | `shouldHandleBeforeUnload` | `boolean` | Determines whether the `beforeunload` event handler should be added to the window, preventing page unload if necessary (`true` by default). | +| `mediaRecorderOptions` | `MediaRecorderOptions` | Configuration options for the MediaRecorder instance. | ##### Returns diff --git a/src/hooks/useVoiceVisualizer.tsx b/src/hooks/useVoiceVisualizer.tsx index ad399f1..9e9c32a 100644 --- a/src/hooks/useVoiceVisualizer.tsx +++ b/src/hooks/useVoiceVisualizer.tsx @@ -20,6 +20,7 @@ function useVoiceVisualizer({ onResumedAudioPlayback, onErrorPlayingAudio, shouldHandleBeforeUnload = true, + mediaRecorderOptions, }: useVoiceVisualizerParams = {}): Controls { const [isRecordingInProgress, setIsRecordingInProgress] = useState(false); const [isPausedRecording, setIsPausedRecording] = useState(false); @@ -167,7 +168,12 @@ function useVoiceVisualizer({ sourceRef.current = audioContextRef.current.createMediaStreamSource(stream); sourceRef.current.connect(analyserRef.current); - mediaRecorderRef.current = new MediaRecorder(stream); + + if (mediaRecorderOptions?.mimeType && !MediaRecorder.isTypeSupported(mediaRecorderOptions.mimeType)) { + throw new Error(`The specified mime type "${mediaRecorderOptions.mimeType}" is not supported by this browser.`); + } + + mediaRecorderRef.current = new MediaRecorder(stream, mediaRecorderOptions); mediaRecorderRef.current.addEventListener( "dataavailable", handleDataAvailable, diff --git a/src/types/types.ts b/src/types/types.ts index 1b2cdab..b2131be 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -120,6 +120,7 @@ export interface useVoiceVisualizerParams { onResumedAudioPlayback?: () => void; onErrorPlayingAudio?: (error: Error) => void; shouldHandleBeforeUnload?: boolean; + mediaRecorderOptions?: MediaRecorderOptions; } export interface UseWebWorkerParams {