Skip to content

Added optional onError parameter #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,7 @@ export enum RecorderErrors {
export function useReactMediaRecorder({
audio = true,
video = false,
onError = () => null,
onStop = () => null,
blobPropertyBag,
screen = false,
Expand Down Expand Up @@ -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");
}
Expand All @@ -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) {
Expand All @@ -226,6 +231,7 @@ export function useReactMediaRecorder({
}
};


const pauseRecording = () => {
if (mediaRecorder.current && mediaRecorder.current.state === "recording") {
setStatus("paused");
Expand Down