npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wmik/use-media-recorder

v1.6.5-beta.0

Published

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

Downloads

9,547

Readme

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