Skip to content

Commit 6cdd6ab

Browse files
authored
New hook: useReactMediaRecorder
1 parent e06d53e commit 6cdd6ab

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

README.md

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-media-recorder :o2: :video_camera: :microphone: :computer:
22

3-
`react-media-recorder` is a fully typed react component with render prop that can be used to:
3+
`react-media-recorder` is a fully typed react component with render prop, or a react hook, that can be used to:
44

55
- Record audio/video
66
- Record screen
@@ -43,6 +43,32 @@ const RecordView = () => (
4343

4444
Since `react-media-recording` uses render prop, you can define what to render in the view. Just don't forget to wire the `startRecording`, `stopRecording` and `mediaBlobUrl` to your component.
4545

46+
## Usage with react hooks
47+
48+
```javascript
49+
import { useReactMediaRecorder } from "react-media-recorder";
50+
51+
const RecordView = () => {
52+
const {
53+
status,
54+
startRecording,
55+
stopRecording,
56+
mediaBlobUrl,
57+
} = useReactMediaRecorder({ video: true });
58+
59+
return (
60+
<div>
61+
<p>{status}</p>
62+
<button onClick={startRecording}>Start Recording</button>
63+
<button onClick={stopRecording}>Stop Recording</button>
64+
<video src={mediaBlobUrl} controls autoplay loop />
65+
</div>
66+
);
67+
};
68+
```
69+
70+
The hook receives an object as argument with the same ReactMediaRecorder options / props (except the `render` function).
71+
4672
### Options / Props
4773

4874
#### audio
@@ -167,9 +193,9 @@ A `function` which unmutes the audio tracks when invoked.
167193

168194
#### mediaBlobUrl
169195

170-
A `blob` url that can be wired to an `<audio />`, `<video />` or an `<a />` element.
196+
A `blob` url that can be wired to an `<audio />`, `<video />` or an `<a />` element.
171197

172-
#### clearBlobUrl
198+
#### clearBlobUrl
173199

174200
A `function` which clears the existing generated blob url (if any)
175201

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-media-recorder",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "A React component based on MediaRecorder() API to record audio/video streams",
55
"main": "index.js",
66
"scripts": {

src/index.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactElement, useCallback, useEffect, useRef, useState } from "react";
22

3-
type ReactMediaRecorderRenderProps = {
3+
export type ReactMediaRecorderRenderProps = {
44
error: string;
55
muteAudio: () => void;
66
unMuteAudio: () => void;
@@ -15,17 +15,19 @@ type ReactMediaRecorderRenderProps = {
1515
clearBlobUrl: () => void;
1616
};
1717

18-
type ReactMediaRecorderProps = {
19-
render: (props: ReactMediaRecorderRenderProps) => ReactElement;
18+
export type ReactMediaRecorderHookProps = {
2019
audio?: boolean | MediaTrackConstraints;
2120
video?: boolean | MediaTrackConstraints;
2221
screen?: boolean;
2322
onStop?: (blobUrl: string, blob: Blob) => void;
2423
blobPropertyBag?: BlobPropertyBag;
2524
mediaRecorderOptions?: MediaRecorderOptions | null;
2625
};
26+
export type ReactMediaRecorderProps = ReactMediaRecorderHookProps & {
27+
render: (props: ReactMediaRecorderRenderProps) => ReactElement;
28+
};
2729

28-
type StatusMessages =
30+
export type StatusMessages =
2931
| "media_aborted"
3032
| "permission_denied"
3133
| "no_specified_media_found"
@@ -40,7 +42,7 @@ type StatusMessages =
4042
| "stopping"
4143
| "stopped";
4244

43-
enum RecorderErrors {
45+
export enum RecorderErrors {
4446
AbortError = "media_aborted",
4547
NotAllowedError = "permission_denied",
4648
NotFoundError = "no_specified_media_found",
@@ -51,15 +53,14 @@ enum RecorderErrors {
5153
NO_RECORDER = "recorder_error",
5254
}
5355

54-
export const ReactMediaRecorder = ({
55-
render,
56+
export function useReactMediaRecorder({
5657
audio = true,
5758
video = false,
5859
onStop = () => null,
5960
blobPropertyBag,
6061
screen = false,
6162
mediaRecorderOptions = null,
62-
}: ReactMediaRecorderProps) => {
63+
}: ReactMediaRecorderHookProps): ReactMediaRecorderRenderProps {
6364
const mediaRecorder = useRef<MediaRecorder | null>(null);
6465
const mediaChunks = useRef<Blob[]>([]);
6566
const mediaStream = useRef<MediaStream | null>(null);
@@ -226,7 +227,7 @@ export const ReactMediaRecorder = ({
226227
}
227228
};
228229

229-
return render({
230+
return {
230231
error: RecorderErrors[error],
231232
muteAudio: () => muteAudio(true),
232233
unMuteAudio: () => muteAudio(false),
@@ -241,5 +242,8 @@ export const ReactMediaRecorder = ({
241242
? new MediaStream(mediaStream.current.getVideoTracks())
242243
: null,
243244
clearBlobUrl: () => setMediaBlobUrl(null),
244-
});
245-
};
245+
};
246+
}
247+
248+
export const ReactMediaRecorder = (props: ReactMediaRecorderProps) =>
249+
props.render(useReactMediaRecorder(props));

0 commit comments

Comments
 (0)