Giter Site home page Giter Site logo

smirnovm91 / react-audio-recorder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samhirtarif/react-audio-recorder

0.0 0.0 0.0 98 KB

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

JavaScript 2.78% TypeScript 87.48% CSS 7.98% HTML 1.76%

react-audio-recorder's Introduction

react-audio-voice-recorder

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

Installation

npm install react-audio-voice-recorder

Usage

AudioRecorder Component (Usage)

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} />
  </React.StrictMode>
);

The component also takes a classes as a prop, allowing you to modify the styles for the entire component or specific portions of it.


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

  import { useAudioRecorder } from 'react-audio-voice-recorder';
  // ...
  // ...
  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 (Working example)

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

react-audio-recorder's People

Contributors

samhirtarif avatar smirnovm91 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.