diff --git a/README.md b/README.md index 6b09e50..5ba8074 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,13 @@ A `function` that would get invoked when the MediaRecorder stops. It'll provide type: `function(blobUrl: string, blob: Blob)` default: `() => null` +#### onError + +A `function` that would get invoked if an error occurred. + +type: `function` +default: `() => null` + #### render A `function` which accepts an object containing fields: `status`, `startRecording`, `stopRecording` and`mediaBlob`. This function would return a react element/component. diff --git a/src/index.ts b/src/index.ts index 39ec623..4e8f499 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ export type ReactMediaRecorderHookProps = { audio?: boolean | MediaTrackConstraints; video?: boolean | MediaTrackConstraints; screen?: boolean; + onError?: () => void; onStop?: (blobUrl: string, blob: Blob) => void; blobPropertyBag?: BlobPropertyBag; mediaRecorderOptions?: MediaRecorderOptions | null; @@ -59,6 +60,7 @@ export enum RecorderErrors { export function useReactMediaRecorder({ audio = true, video = false, + onError = () => null, onStop = () => null, blobPropertyBag, screen = false, @@ -191,10 +193,7 @@ export function useReactMediaRecorder({ mediaRecorder.current = new MediaRecorder(mediaStream.current); mediaRecorder.current.ondataavailable = onRecordingActive; mediaRecorder.current.onstop = onRecordingStop; - mediaRecorder.current.onerror = () => { - setError("NO_RECORDER"); - setStatus("idle"); - }; + mediaRecorder.current.onerror = onRecordingError; mediaRecorder.current.start(); setStatus("recording"); } @@ -217,6 +216,12 @@ export function useReactMediaRecorder({ onStop(url, blob); }; + const onRecordingError = () => { + setError("NO_RECORDER"); + setStatus("idle"); + onError(); + } + const muteAudio = (mute: boolean) => { setIsAudioMuted(mute); if (mediaStream.current) { @@ -226,6 +231,7 @@ export function useReactMediaRecorder({ } }; + const pauseRecording = () => { if (mediaRecorder.current && mediaRecorder.current.state === "recording") { setStatus("paused");