1.0.5 • Published 4 months ago

react-voice-record v1.0.5

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

React Voice Record

A customizable voice recording component for React applications with pause/resume functionality, timer, and style customization.

Voice Recorder Demo

Features

  • 🎙 Start/stop recording with media constraints
  • ⏯ Pause/resume recording functionality
  • ⏲ Customizable timer with multiple formats
  • 🎨 Style customization via CSS-in-JS or CSS classes
  • 📁 Audio chunking configuration
  • ⏰ Max duration limit
  • ♿ Accessible ARIA labels
  • 🔧 Custom icon support
  • 🛠 Error handling callbacks
  • 📦 Zero dependencies

Installation

npm install react-voice-record
or 
yarn add react-voice-record

Basic Usage

import React from 'react';
import VoiceRecorder from 'react-voice-record';

const App = () => {
  const handleRecordingStart = () => {
    console.log('Recording started');
  };

  const handleRecordingStop = (audioBlob) => {
    console.log('Recording stopped', audioBlob);
    // You can use the audioBlob to play or upload the recorded audio
  };

  const handleRecordingPause = () => {
    console.log('Recording paused');
  };

  const handleRecordingResume = () => {
    console.log('Recording resumed');
  };

  const handleError = (error) => {
    console.error('An error occurred during recording', error);
  };

  const handleMaxDuration = () => {
    console.log('Maximum recording duration reached');
  };

  return (
    <div>
      <h1>Voice Recorder Demo</h1>
      <VoiceRecorder
        onRecordingStart={handleRecordingStart}
        onRecordingStop={handleRecordingStop}
        onRecordingPause={handleRecordingPause}
        onRecordingResume={handleRecordingResume}
        onError={handleError}
        onMaxDuration={handleMaxDuration}
        maxDuration={300} // Maximum recording duration in seconds (e.g., 5 minutes)
        timerFormat="mm:ss"
        showTimer={true}
      />
    </div>
  );
};

export default App;

Props References

PropTypeDefaultDescription
onRecordingStartfunction-Called when recording starts
onRecordingStopfunction-Receives audio blob when recording stops
onRecordingPausefunction-Called when recording pauses
onRecordingResumefunction-Called when recording resumes
onErrorfunction-Error handler callback
onMaxDurationfunction-Triggered when max duration is reached
stylesobject{}Custom CSS-in-JS styles
classNamesobject{}Custom CSS classNames
startIconReactNode🎤Start button icon
stopIconReactNodeStop button icon
pauseIconReactNodePause button icon
resumeIconReactNodeResume button icon
maxDurationnumbernullMax recording duration (seconds)
timerFormatstring'mm:ss'Time format (hh:mm:ss, mm:ss, ss)
showTimerbooleantrueShow/hide timer display
disabledbooleanfalseDisable all controls
audioConstraintsobject{ echoCancellation: true, noiseSuppression: true, autoGainControl: true }Audio constraints
mediaRecorderOptionsobject{ mimeType: 'audio/webm;codecs=opus' }MediaRecorder options
chunkDurationnumber1000Data chunking interval (ms)

Styling Guide

  1. CSS-in-JS Styling
<VoiceRecorder 
  styles={{
    container: {
      backgroundColor: '#ffffff',
      boxShadow: '0 2px 8px rgba(0,0,0,0.1)'
    },
    button: {
      backgroundColor: '#3b82f6',
      '&:hover:not(:disabled)': {
        transform: 'scale(1.15)'
      }
    },
    stopButton: {
      backgroundColor: '#ef4444'
    }
  }}
/>

2. CSS Classes

<VoiceRecorder
  classNames={{
    container: 'custom-container',
    button: 'primary-button',
    stopButton: 'danger-button',
    timer: 'custom-timer'
  }}
/>

Custom Icons

import { Mic, StopCircle, PauseCircle, PlayCircle } from 'react-feather';

<VoiceRecorder
  startIcon={<Mic size={20} />}
  stopIcon={<StopCircle size={20} />}
  pauseIcon={<PauseCircle size={20} />}
  resumeIcon={<PlayCircle size={20} />}
/>

Timer Formats

<VoiceRecorder
  timerFormat="hh:mm:ss"  // 00:05:23
  timerFormat="mm:ss"     // 05:23 (default)
  timerFormat="ss"        // 323 seconds
/>

Audio Configuration

<VoiceRecorder
  audioConstraints={{
    sampleRate: 44100,
    channelCount: 1,
    noiseSuppression: false
  }}
  mediaRecorderOptions={{
    mimeType: 'audio/mp3',
    audioBitsPerSecond: 128000
  }}
  chunkDuration={2000}
/>

Error Handling

<VoiceRecorder
  onError={(error) => {
    console.error('Recording error:', error);
    alert('Microphone access required!');
  }}
/>

License

MIT © Mostafijur Rahman. Free for commercial and personal use.

1.0.5

4 months ago

1.0.4

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago