0.0.9 • Published 3 years ago

react-hook-recorder v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

react-hook-recorder

A simple react hook using MediaRecorder API

Demo

https://codesandbox.io/s/react-hook-recorder-gbz6z

Example

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&nbsp;:
          <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>&nbsp;
        {status}
      </div>
    </div>
  );
}

export default Recorder;

API

useRecorder

Args (mediaStreamConstraints: MediaStreamConstraints, mediaRecorderOptions: MediaRecorderOptions)

PropertyRequiredTypeDescription
mediaStreamConstraintsfalseobjectMediaStreamConstraints object
mediaRecorderOptionsfalseobjectMediaRecorder object

Returns (object)

PropertyTypeArgsDescription
mediaRecorderMediaRecorderMediaRecorder instance ref
streamMediaStreamMediaStream instance ref
startRecordingfunctionfunction to start recording
stopRecordingfunctionfunction(blob: Blob, url: string) => voidfunction to stop recording
registerfunctionHTMLVideoElementfunction to register video element
statusRecorderStatusreturn recorder status
errorRecorderErrorreturn recorder error

Types

enum RecorderStatus : "idle" | "init" | "recording"

enum RecorderError : "stream-init" | "recorder-init"

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago