From 4a679ae7ef0c6640dde581c5a66ccedf9b011aea Mon Sep 17 00:00:00 2001 From: Arman Takmazyan Date: Wed, 24 Nov 2021 18:34:03 +0400 Subject: [PATCH 1/3] Added onError prop --- src/index.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 39ec623..f1963a9 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"); } @@ -226,6 +225,12 @@ export function useReactMediaRecorder({ } }; + const onRecordingError = () => { + setError("NO_RECORDER"); + setStatus("idle"); + onError(); + } + const pauseRecording = () => { if (mediaRecorder.current && mediaRecorder.current.state === "recording") { setStatus("paused"); From ccef5ed082f09795288ddbd44542f7227ca72d5a Mon Sep 17 00:00:00 2001 From: Arman Takmazyan Date: Wed, 24 Nov 2021 18:44:12 +0400 Subject: [PATCH 2/3] Changed onError --- src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1963a9..4e8f499 100644 --- a/src/index.ts +++ b/src/index.ts @@ -216,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) { @@ -225,11 +231,6 @@ export function useReactMediaRecorder({ } }; - const onRecordingError = () => { - setError("NO_RECORDER"); - setStatus("idle"); - onError(); - } const pauseRecording = () => { if (mediaRecorder.current && mediaRecorder.current.state === "recording") { From 8613562968964e4438d4d7b60ad23ea19cee94a6 Mon Sep 17 00:00:00 2001 From: Arman Takmazyan Date: Sat, 4 Dec 2021 22:39:23 +0400 Subject: [PATCH 3/3] update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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.