2.2.0 • Published 7 months ago

react-audio-voice-recorder v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

react-audio-voice-recorder

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

NPM downloads Run ESlint Run Unit tests

Installation

npm install react-audio-voice-recorder
yarn add react-audio-voice-recorder

Migrating from v1 → v2

Breaking changes

  • In v2 the AudioRecorder prop downloadFileExtension no longer supports mp3 and wav without the website using this package being cross-origin isolated. This change was made in order to fix issue #54 in v1.2.1

Usage

AudioRecorder Component (See it in action)

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-voice-recorder';

const addAudioElement = (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}
      audioTrackConstraints={{
        noiseSuppression: true,
        echoCancellation: true,
      }} 
      downloadOnSavePress={true}
      downloadFileExtension="webm"
    />
  </React.StrictMode>
);
PropsDescriptionDefaultOptional
onRecordingCompleteA method that gets called when "Save recording" option is pressedN/AYes
audioTrackConstraintsTakes a subset of MediaTrackConstraints that apply to the audio trackN/AYes
onNotAllowedOrFoundThis gets called when the getUserMedia promise is rejected. It takes the resultant DOMException as its parameterN/AYes
downloadOnSavePressA boolean value that determines if the recording should be downloaded when "Save recording" option is pressedfalseYes
downloadFileExtensionThe file extension to be used for the downloaded file. Allowed values are webm, mp3 and wav. In order to use mp3 or wav please ensure that your website is cross-origin isolated. Further readingwebmYes
showVisualizerDisplays a waveform visualization for the audio when set to truefalseYes
classesThis allows class names to be passed to modify the styles for the entire component or specific portions of itN/AYes

NOTE: In order for mp3 and wav downloading to work properly, your website needs to be cross-origin isolated. This is necessary because this package uses FFmpeg which internally uses SharedArrayBuffer that requires cross-origin isolation


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

ParamsDescriptionOptional
audioTrackConstraintsTakes a subset of MediaTrackConstraints that apply to the audio trackYes
onNotAllowedOrFoundThis gets called when the getUserMedia promise is rejected. It takes the resultant DOMException as its parameterYes

The hook returns the following:

IdentifiersDescription
startRecordingInvoking this method starts the recording. Sets isRecording to true
stopRecordingInvoking this method stops the recording in progress and the resulting audio is made available in recordingBlob. Sets isRecording to false
togglePauseResumeInvoking this method would pause the recording if it is currently running or resume if it is paused. Toggles the value isPaused
recordingBlobThis is the recording blob that is created after stopRecording has been called
isRecordingA boolean value that represents whether a recording is currently in progress
isPausedA boolean value that represents whether a recording in progress is paused
recordingTimeNumber of seconds that the recording has gone on. This is updated every second
mediaRecorderThe current mediaRecorder in use. Can be undefined in case recording is not in progress

Sample usage of hook

  import { useAudioRecorder } from 'react-audio-voice-recorder';
  // ...
  // ...
  const {
    startRecording,
    stopRecording,
    togglePauseResume,
    recordingBlob,
    isRecording,
    isPaused,
    recordingTime,
    mediaRecorder
  } = useAudioRecorder();

  useEffect(() => {
    if (!recordingBlob) return;

    // recordingBlob will be present at this point after 'stopRecording' has been called
  }, [recordingBlob])

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 (See it in action)

import { AudioRecorder, useAudioRecorder } from 'react-audio-voice-recorder';

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>
  )
}

NOTE: When using both AudioRecorder and useAudioRecorder in combination, the audioTrackConstraints and onNotAllowedOrFound should be provided in the useAudioRecorder hook

2.2.0

7 months ago

1.2.0

11 months ago

1.1.7

11 months ago

1.2.1

11 months ago

2.1.2

9 months ago

2.1.1

10 months ago

2.0.2

10 months ago

2.1.0

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

2.1.0-beta.1

10 months ago

2.1.0-beta.0

10 months ago

2.1.0-beta.2

10 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.1.6

11 months ago

1.0.7

1 year ago

1.1.5

11 months ago

1.0.6

1 year ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.0.11

12 months ago

1.0.10

1 year ago

1.0.5

1 year ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago