Coder Social home page Coder Social logo

ntdat104 / use-media-recorder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wmik/use-media-recorder

0.0 0.0 0.0 51 KB

React based hooks to utilize the media recorder api for audio, video and screen recording

License: MIT License

JavaScript 100.00%

use-media-recorder's Introduction

use-media-recorder

React based hooks to utilize the MediaRecorder API for audio, video and screen recording.

Features

  • ๐Ÿ‘€ Familiar API - Extends the MediaRecorder/MediaStream API with minimal abstraction making it easy to use.
  • ๐Ÿ”ด Media recording - Supports audio ๐ŸŽค, video ๐ŸŽฅ & screen ๐Ÿ–ฅ๏ธ recording.
  • ๐ŸŽ›๏ธ Configurable - Adjust settings to match your recording requirements.
  • ๐Ÿ’… Headless - Build your own custom user interface to fit your style.

Installation

npm install @wmik/use-media-recorder

Example

import React from 'react';
import useMediaRecorder from '@wmik/use-media-recorder';

function Player({ srcBlob, audio }) {
  if (!srcBlob) {
    return null;
  }

  if (audio) {
    return <audio src={URL.createObjectURL(srcBlob)} controls />;
  }

  return (
    <video
      src={URL.createObjectURL(srcBlob)}
      width={520}
      height={480}
      controls
    />
  );
}

function ScreenRecorderApp() {
  let {
    error,
    status,
    mediaBlob,
    stopRecording,
    getMediaStream,
    startRecording
  } = useMediaRecorder({
    recordScreen: true,
    blobOptions: { type: 'video/webm' },
    mediaStreamConstraints: { audio: true, video: true }
  });

  return (
    <article>
      <h1>Screen recorder</h1>
      {error ? `${status} ${error.message}` : status}
      <section>
        <button
          type="button"
          onClick={getMediaStream}
          disabled={status === 'ready'}
        >
          Share screen
        </button>
        <button
          type="button"
          onClick={startRecording}
          disabled={status === 'recording'}
        >
          Start recording
        </button>
        <button
          type="button"
          onClick={stopRecording}
          disabled={status !== 'recording'}
        >
          Stop recording
        </button>
      </section>
      <Player srcBlob={mediaBlob} />
    </article>
  );
}

Demo

Live demo example

API

useMediaRecorder (Default export)

Creates a custom media recorder object using the MediaRecorder API.

Parameters (MediaRecorderProps)

Property Type Description
blobOptions BlobPropertyBag Options used for creating a Blob object.
recordScreen boolean Enable/disable screen capture.
customMediaStream MediaStream Custom stream e.g canvas.captureStream
onStart function Callback to run when recording starts.
onStop function Callback to run when recording stops. Accepts a Blob object as a parameter.
onError function Callback to run when an error occurs while recording. Accepts an error object as a parameter.
onDataAvailable function Callback to run when recording data exists.
mediaRecorderOptions object Options used for creating MediaRecorder object.
mediaStreamConstraints* MediaStreamConstraints Options used for creating a MediaStream object from getDisplayMedia and getUserMedia.

NOTE: * means it is required

Returns (MediaRecorderHookOptions)

Property Type Description
error Error Information about an operation failure. Possible exceptions
status string Current state of recorder. One of idle, acquiring_media, ready, recording, paused,stopping, stopped, failed.
mediaBlob Blob Raw media data.
isAudioMuted boolean Indicates whether audio is active/inactive.
stopRecording function End a recording.
getMediaStream function Request for a media source. Camera, mic and/or screen access. Returns instance of requested media source or customMediaStream if was provided in initializing.
clearMediaStream function Resets the media stream object to null.
clearMediaBlob function Resets the media blob to null.
startRecording function(timeSlice?) Begin a recording. Optional argument timeSlice controls chunk size.
pauseRecording function Stop without ending a recording allowing the recording to continue later.
resumeRecording function Continue a recording from a previous pause.
muteAudio function Disable audio.
unMuteAudio function Enable audio.
liveStream MediaStream Real-time stream of current recording.

More examples

function LiveStreamPreview({ stream }) {
  let videoPreviewRef = React.useRef();

  React.useEffect(() => {
    if (videoPreviewRef.current && stream) {
      videoPreviewRef.current.srcObject = stream;
    }
  }, [stream]);

  if (!stream) {
    return null;
  }

  return <video ref={videoPreviewRef} width={520} height={480} autoPlay />;
}

<LiveStreamPreview stream={liveStream} />

Related

License

MIT ยฉ2020

use-media-recorder's People

Contributors

wmik avatar martynassapoka avatar amir-alipour avatar no-1ne avatar chiefgui avatar janmisker avatar slowestmonkey avatar swissspidy avatar itsnotrisky avatar eminvergil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.