Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-audio-voice-recorder

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-audio-voice-recorder

An audio recording helper for React. Provides a component and a hook to help with audio recording.

  • 0.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21K
decreased by-13.1%
Maintainers
1
Weekly downloads
 
Created
Source

react-audio-recorder

An audio recording helper for React. Provides a component and a hook to help with audio recording.

Installation

npm install react-audio-recorder

Usage

AudioRecorder Component

You can use an out-of-the-box component that takes onRecordingComplete method as a prop and calls it when you save the recording

import React from "react";
import ReactDOM from "react-dom/client";
import AudioRecorder from "react-audio-recorder";

const addAudioElement = (blob: Blob) => {
  const url = URL.createObjectURL(blob);
  const audio = document.createElement("audio");
  audio.src = url;
  audio.controls = true;
  document.body.appendChild(audio);
};

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <AudioRecorder onRecordingComplete={addAudioElement} />
  </React.StrictMode>
);

useAudioRecorder hook

If you prefer to build up your own UI but take advantage of the implementation provided by this package, you can use this hook instead of the component

The hook returns the following:

startRecording

Calling this method would result in the recording to start. Sets isRecording to true

stopRecording

This results in a recording in progress being stopped and the resulting audio being present in recordingBlob. Sets isRecording to false

togglePauseResume

Calling this method would pause the recording if it is currently running or resume if it is paused. Toggles the value isPaused

recordingBlob

This is the recording blob that is created after stopRecording has been called

isRecording

A boolean value that represents whether a recording is currently in progress

isPaused

A boolean value that represents whether a recording in progress is paused

recordingTime

Number of seconds that the recording has gone on. This is updated every second

Sample usage of hook

  const {
    startRecording,
    stopRecording,
    togglePauseResume,
    recordingBlob,
    isRecording,
    isPaused,
    recordingTime,
  } = useAudioRecorder();

Combine the useAudioRecorder hook and the AudioRecorder component

This is for scenarios where you would wish to control the AudioRecorder component from outside the component. You can call the useAudioRecorder and pass the object it returns to the recorderControls of the AudioRecorder. This would enable you to control the AudioRecorder component from outside the component as well

Sample usage
import AudioRecorder from "./components/AudioRecordingComponent";
import useAudioRecorder from "./hooks/useAudioRecorder";

const ExampleComponent = () => {
  const recorderControls = useAudioRecorder()
  const addAudioElement = (blob) => {
    const url = URL.createObjectURL(blob);
    const audio = document.createElement("audio");
    audio.src = url;
    audio.controls = true;
    document.body.appendChild(audio);
  };

  return (
    <div>
      <AudioRecorder 
        onRecordingComplete={(blob) => addAudioElement(blob)}
        recorderControls={recorderControls}
      />
      <button onClick={recorderControls.stopRecording}>Stop recording</button>
    </div>
  )
}

Keywords

FAQs

Package last updated on 03 Sep 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc