1.3.11 • Published 1 year ago

react-visual-audio-recorder v1.3.11

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

ReactVisualAudioRecorder

A simple audio recorder compatible with a large part of the browsers.

ReactVisualAudioRecorder component

This component has been designed to render a sound wave when recording audio. The recording control functions are done via the ref of the component and the recording via the onData props for controlled and continuous recording or via the onChange props for data recording during breaks (pause/stop).

API Reference

ReactVisualAudioRecorder component props

None of the props are required

ParameterTypeDefaultDescription
widthnumber640Width of the canvas.
heightnumber100Height of the canvas.
onStart(MediaRecorder, AudioContext, MediaStream, AnalyserNode) => voidCalled when the recording is started
onStop(BlobObject) => voidCalled when the recording is stopped.
onChange(BlobObject) => voidCalled when the recording is stopped or paused.
onData(blob: Blob) => void;Called during the recording. Sending all chunks as blob during the recording.
handleStatus(status: "pause" "recording" "stopped") => voidFunction that handle status of the recording instance.
noVisualisationbooleanfalseShow visualisation canvas.
showOnlyOnRecordbooleanfalseShow visualisation only if microphone is recording.
audioBitsPerSecondnumber128000Quality of the recording in bytes/second.
echoCancellationbooleantrueReduces the echo of the recording.
autoGainControlbooleantrueControl circuit in an amplifier or a chain of amplifiers.
noiseSuppressionbooleantrueSuppresses background noise of the recording.
channelCountnumber2Number of channels recorded. Default is left/right for a stereo recording.
frequencySizenumber512Sine wave spacing.
mimeTypestringDepend of browserhttps://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#audio_and_video_types
backgroundColorstringrgba(255, 255, 255, 0.5)BackgroundColor of the curve.
strokeColorstring`#000000 | Color of the curve.
classNamestring
ref(ref: ForwardedRef<ReactVisualAudioRecorderRefHandler>) => voidRefs of the internal component functions. Setted with useImperativeHandle.

ReactVisualAudioRecorderRefHandler

functionTypeDescription
start() => voidStart recording.
stop() => voidStop recording.
reset() => voidReset recording.
pause() => voidPause recording.
resume() => voidResume recording.
getFileExtension() => stringReturn the correct extension for the mimeType,

Usage/Examples

import React, { useRef, useState } from "react";
import ReactVisualAudioRecorder from "react-visual-audio-recorder";
import type {
  ReactVisualAudioRecorderRefHandler,
  ReactVisualAudioRecorderBlobObject,
} from "react-visual-audio-recorder";

const visualizerWidth = 300;
const visualizerHeight = 70;

export default function App() {
  const [url, setUrl] = useState<string | null>(null);
  const [status, setStatus] = useState<"pause" | "recording" | "stopped">("stopped");

  const audioRecorder = useRef<ReactVisualAudioRecorderRefHandler | null>(null);

  function toggleRecording() {
    if (status === "stopped") audioRecorder.current?.start();
    else if (status === "pause") audioRecorder.current?.resume();
    else if (status === "recording") audioRecorder.current?.pause();
  }

  function onChange(blobObject: ReactVisualAudioRecorderBlobObject) {
    if (!blobObject) return;
    setUrl(blobObject.blobURL);
  }

  function reset() {
    audioRecorder.current?.reset();
    setUrl(null);
  }

  return (
    <div style={{ display: "flex", flexDirection: "column" }}>
      <div style={{ width: visualizerWidth, height: visualizerHeight }}>
        <ReactVisualAudioRecorder
          ref={audioRecorder}
          width={visualizerWidth}
          height={visualizerHeight}
          onChange={onChange}
          handleStatus={setStatus}
        />
      </div>
      <div>
        <button onClick={toggleRecording}>
          {status === "stopped" ? "Start recording" : status === "pause" ? "Resume" : "Pause"}
        </button>
        <button onClick={() => reset()} disabled={!url || status === "recording"}>
          Reset
        </button>
        {url ? (
          <a href={url} download={`file.${audioRecorder.current?.getFileExtension() || "ogg"}`}>
            <button>Download</button>
          </a>
        ) : null}
      </div>
      {url ? <audio src={url || ""} controls={true} /> : null}
    </div>
  );
}

License

MIT

1.3.11

1 year ago

1.3.10

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago