Socket
Socket
Sign inDemoInstall

@wmik/use-media-recorder

Package Overview
Dependencies
1
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @wmik/use-media-recorder

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


Version published
Weekly downloads
1.4K
increased by4.8%
Maintainers
1
Install size
16.9 kB
Created
Weekly downloads
 

Readme

Source

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)
PropertyTypeDescription
blobOptionsBlobPropertyBagOptions used for creating a Blob object.
recordScreenbooleanEnable/disable screen capture.
customMediaStreamMediaStreamCustom stream e.g canvas.captureStream
onStartfunctionCallback to run when recording starts.
onStopfunctionCallback to run when recording stops. Accepts a Blob object as a parameter.
onErrorfunctionCallback to run when an error occurs while recording. Accepts an error object as a parameter.
onDataAvailablefunctionCallback to run when recording data exists.
mediaRecorderOptionsobjectOptions used for creating MediaRecorder object.
mediaStreamConstraints*MediaStreamConstraintsOptions used for creating a MediaStream object from getDisplayMedia and getUserMedia.

NOTE: * means it is required

Returns (MediaRecorderHookOptions)
PropertyTypeDescription
errorErrorInformation about an operation failure. Possible exceptions
statusstringCurrent state of recorder. One of idle, acquiring_media, ready, recording, paused,stopping, stopped, failed.
mediaBlobBlobRaw media data.
isAudioMutedbooleanIndicates whether audio is active/inactive.
stopRecordingfunctionEnd a recording.
getMediaStreamfunctionRequest for a media source. Camera, mic and/or screen access. Returns instance of requested media source or customMediaStream if was provided in initializing.
clearMediaStreamfunctionResets the media stream object to null.
clearMediaBlobfunctionResets the media blob to null.
startRecordingfunction(timeSlice?)Begin a recording. Optional argument timeSlice controls chunk size.
pauseRecordingfunctionStop without ending a recording allowing the recording to continue later.
resumeRecordingfunctionContinue a recording from a previous pause.
muteAudiofunctionDisable audio.
unMuteAudiofunctionEnable audio.
liveStreamMediaStreamReal-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} />

License

MIT ©2020

Keywords

FAQs

Last updated on 12 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc