A simple react hook using MediaRecorder API
https://codesandbox.io/s/react-hook-recorder-gbz6z
import React, { useCallback, useState } from "react";
import useRecorder from "react-hook-recorder";
function Recorder() {
const [url, setUrl] = useState("");
const onStop = useCallback((blob, blobUrl) => {
setUrl(blobUrl);
}, []);
const { startRecording, stopRecording, register, status } = useRecorder();
return (
<div>
<video ref={register} autoPlay muted playsInline />
{url && (
<>
Recorded video :
<video controls src={url} />
</>
)}
{status !== "init" && (
<>
<button onClick={startRecording} disabled={status === "recording"}>
Start Recording
</button>
<button
onClick={stopRecording(onStop)}
disabled={status !== "recording"}
>
Stop Recording
</button>
</>
)}
<div>
<strong>Status :</strong>
{status}
</div>
</div>
);
}
export default Recorder;
Property | Required | Type | Description |
---|---|---|---|
mediaStreamConstraints | false | object |
MediaStreamConstraints object |
mediaRecorderOptions | false | object |
MediaRecorder object |
Property | Type | Args | Description |
---|---|---|---|
mediaRecorder | MediaRecorder |
MediaRecorder instance ref | |
stream | MediaStream |
MediaStream instance ref | |
startRecording | function |
function to start recording | |
stopRecording | function |
function(blob: Blob, url: string) => void |
function to stop recording |
register | function |
HTMLVideoElement |
function to register video element |
status | RecorderStatus |
return recorder status | |
error | RecorderError |
return recorder error |