Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/hooks/useVoiceVisualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function useVoiceVisualizer({
onResumedAudioPlayback,
onErrorPlayingAudio,
shouldHandleBeforeUnload = true,
mediaRecorderOptions,
}: useVoiceVisualizerParams = {}): Controls {
const [isRecordingInProgress, setIsRecordingInProgress] = useState(false);
const [isPausedRecording, setIsPausedRecording] = useState(false);
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface useVoiceVisualizerParams {
onResumedAudioPlayback?: () => void;
onErrorPlayingAudio?: (error: Error) => void;
shouldHandleBeforeUnload?: boolean;
mediaRecorderOptions?: MediaRecorderOptions;
}

export interface UseWebWorkerParams<T> {
Expand Down