Install react-media-recorder
masterYou can install react-media-recorder using npm or yarn.
npm i react-media-recorderyarn add react-media-recorderrepository·master·Indexed 20 days ago
https://github.com/deltacircuit/react-media-recorderA React library providing a high-level interface for the Web MediaRecorder API to record audio, video, or screen captures. It offers both a render-prop component (ReactMediaRecorder) and a React hook (useReactMediaRecorder) to manage recording state, controls, and media blob URLs. Version 1.7.2.
You can install react-media-recorder using npm or yarn.
npm i react-media-recorderyarn add react-media-recorderTo show a live preview of the camera/screen, use the previewStream provided in the render function. Attach this stream to a <video> element's srcObject. Note that previewStream is muted by default to prevent audio feedback loops.
const VideoPreview = ({ stream }: { stream: MediaStream | null }) => {
const videoRef = useRef<HTMLVideoElement>(null);
useEffect(() => {
if (videoRef.current && stream) {
videoRef.current.srcObject = stream;
}
}, [stream]);
if (!stream) {
return null;
}
return <video ref={videoRef} width={500} height={500} autoPlay controls />;
};
const App = () => (
<ReactMediaRecorder
video
render={({ previewStream }) => {
return <VideoPreview stream={previewStream} />;
}}
/>
);The following options can be passed to both the ReactMediaRecorder component and the useReactMediaRecorder hook:
| Option | Type | Default | Description |
|---|---|---|---|
audio | boolean or object | true | Enables audio recording. Can be a MediaTrackConstraints object. |
video | boolean or object | false | Enables video recording. Can be a MediaTrackConstraints object. |
screen | boolean | false | Records the current screen. If both screen and video are provided, screen takes precedence. |
blobPropertyBag | object | video: {type: "video/mp4"} or audio: {type: "audio/wav"} | Specifies MIME type and line endings for the resulting blob. |
mediaRecorderOptions | object | {} | Options passed directly to the MediaRecorder API. Overrides audio/video MIME types. |
customMediaStream | MediaStream | undefined | An optional existing media stream to use. |
onStart | function() | () => null | Invoked when recording starts. |
onStop | function(blobUrl: string, blob: Blob) | () => null | Invoked when recording stops. Provides the blob URL and the Blob. |
stopStreamsOnStop | boolean | true | Whether to stop all streams when recording stops. |
askPermissionOnMount | boolean | false | If true, requests media permissions immediately upon mounting. |
preferCurrentTab | boolean | false | If true, hints the browser to prioritize the current tab in screen sharing. |
selfBrowserSurface | 'include' | 'exclude' | undefined | undefined | Specifies if the current tab should be included/excluded in capture choices. |
The ReactMediaRecorder component uses a render prop pattern. You provide a render function that receives an object containing the recording state and controls. You must wire up startRecording, stopRecording, and mediaBlobUrl to your UI elements to make them functional.
import { ReactMediaRecorder } from "react-media-recorder";
const RecordView = () => (
<div>
<ReactMediaRecorder
video
render={({ status, startRecording, stopRecording, mediaBlobUrl }) => (
<div>
<p>{status}</p>
<button onClick={startRecording}>Start Recording</button>
<button onClick={stopRecording}>Stop Recording</button>
<video src={mediaBlobUrl} controls autoPlay loop />
</div>
)}
/>
</div>
);For a hook-based approach, use useReactMediaRecorder. The hook accepts an options object with the same properties as the ReactMediaRecorder component (excluding the render prop) and returns the recording state and controls.
import { useReactMediaRecorder } from "react-media-recorder";
const RecordView = () => {
const { status, startRecording, stopRecording, mediaBlobUrl } =
useReactMediaRecorder({ video: true });
return (
<div>
<p>{status}</p>
<button onClick={startRecording}>Start Recording</button>
<button onClick={stopRecording}>Stop Recording</button>
<video src={mediaBlobUrl} controls autoPlay loop />
</div>
);
};The render function (or the useReactMediaRecorder hook return value) provides access to the following properties:
status: A string enum representing the current state:idle, acquiring_media, recording, stopping, stopped, media_aborted, permission_denied, no_specified_media_found, media_in_use, invalid_media_constraints, no_constraints, recorder_error.error: A string enum describing the error state:media_aborted, permission_denied, no_specified_media_found, media_in_use, invalid_media_constraints, no_constraints, recorder_error.startRecording: Starts the recording.stopRecording: Stops the recording.pauseRecording: Pauses the recording.resumeRecording: Resumes a paused recording.muteAudio: Mutes audio tracks.unmuteAudio: Unmutes audio tracks.clearBlobUrl: Clears the mediaBlobUrl and resets to idle state.mediaBlobUrl: A URL for the recorded blob, suitable for <video>, <audio>, or <a> elements.isMuted: Boolean indicating if audio is currently muted.previewStream: A MediaStream for live video preview (Note: this stream is muted by design to prevent feedback).previewAudioStream: A MediaStream for live audio, useful for Web Audio API visualizations.The ReactMediaRecorder component is a render-prop component that wraps the useReactMediaRecorder hook. It is useful for a quick implementation where you want to define your UI within a single component using a render function.
It accepts all the same configuration props as the hook, plus a render prop which receives the recorder's state and control functions.
import { ReactMediaRecorder } from 'react-media-recorder';
const MyRecorder = () => (
<ReactMediaRecorder
audio={true}
video={true}
render={({ startRecording, stopRecording, status }) => (
<div>
<p>Status: {status}</p>
<button onClick={startRecording}>Start</button>
<button onClick={stopRecording}>Stop</button>
</div>
)}
/>
);The useReactMediaRecorder hook is the core logic provider for media recording. It manages media stream acquisition, the MediaRecorder lifecycle, and provides state updates for recording status, errors, and the resulting media blob URL.
It supports audio, video, and screen recording. You can pass custom constraints for audio/video tracks or provide a customMediaStream to bypass the default acquisition logic.
import { useReactMediaRecorder } from 'react-media-recorder';
const {
startRecording,
stopRecording,
status,
mediaBlobUrl
} = useReactMediaRecorder({
audio: true,
video: true,
onStop: (blobUrl, blob) => {
console.log('Recording stopped:', blobUrl);
}
});When calling useReactMediaRecorder, you can pass a ReactMediaRecorderHookProps object to configure the recording behavior:
audio: boolean or MediaTrackConstraints. Defaults to true.video: boolean or MediaTrackConstraints. Defaults to false.screen: boolean. If true, uses getDisplayMedia for screen capture.selfBrowserSurface: 'include' | 'exclude' | undefined. Controls if the current tab is offered in screen capture.preferCurrentTab: boolean. If true, hints the browser to make the current tab a prominent option.onStart: Callback function called when recording starts.onStop: Callback function (blobUrl: string, blob: Blob) => void called when recording stops.mediaRecorderOptions: MediaRecorderOptions passed to the underlying MediaRecorder.customMediaStream: A MediaStream to use instead of acquiring a new one.stopStreamsOnStop: boolean. If true, stops all media tracks when stopRecording is called. Defaults to true.askPermissionOnMount: boolean. If true, attempts to acquire media permissions immediately on mount.The recorder uses specific enums and types to communicate state and errors.
StatusMessages:
"media_aborted", "permission_denied", "no_specified_media_found", "media_in_use", "invalid_media_constraints", "no_constraints", "recorder_error", "idle", "acquiring_media", "delayed_start", "recording", "stopping", "stopped", "paused".
RecorderErrors (mapped to the error prop):
AbortError: "media_aborted"NotAllowedError: "permission_denied"NotFoundError: "no_specified_media_found"NotReadableError: "media_in_use"OverconstrainedError: "invalid_media_constraints"TypeError: "no_constraints"NO_RECORDER: "recorder_error"The render function of the ReactMediaRecorder component (and the return value of the useReactMediaRecorder hook) provides the following properties:
error: A string representing the current error (mapped from RecorderErrors).muteAudio: Function to mute the audio track.unMuteAudio: Function to unmute the audio track.startRecording: Function to begin recording.pauseRecording: Function to pause the current recording.resumeRecording: Function to resume a paused recording.stopRecording: Function to stop the recording.mediaBlobUrl: The URL of the recorded media (string or undefined).status: The current recording StatusMessages.isAudioMuted: Boolean indicating if audio is currently muted.previewStream: A MediaStream containing the video tracks for previewing.previewAudioStream: A MediaStream containing the audio tracks for previewing.clearBlobUrl: Function to revoke the mediaBlobUrl and reset the status to idle.