npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-audio-recorder-hook

v1.0.7

Published

A powerful TypeScript-based React hook that provides complete audio recording capabilities with pause/resume functionality, recording management, and audio processing

Downloads

166

Readme

React Audio Recorder Hook

A powerful, TypeScript-based React hook for recording, managing, and processing audio in web applications with minimal setup and maximum flexibility.

Features

  • Simple Integration: Add professional audio recording capabilities to your React app with just a few lines of code
  • Full Recording Control: Start, stop, pause, resume, and cancel recordings with ease
  • Customizable Audio Settings: Configure audio constraints, chunk intervals, and MIME types
  • Recording Management: Track recording duration and state (recording/paused) automatically
  • Audio Processing: Save recordings as blobs with URLs for immediate playback or download
  • TypeScript Support: Fully typed API with comprehensive interfaces for type safety
  • Modern Browser Support: Works across all major browsers that support the MediaRecorder API
  • Zero Dependencies: Lightweight implementation built only on React hooks
  • Responsive Design: Perfect for both desktop and mobile web applications
  • Audio Visualization: Example component demonstrates how to add audio waveform visualization
  • Comprehensive Tests: Complete test suite ensures reliability

Support

If you find this package helpful, you can support its development by buying me a coffee:

Buy Me A Coffee

Installation

npm install react-audio-recorder-hook
# or
yarn add react-audio-recorder-hook

Documentation

For comprehensive documentation, see the following resources:

Basic Usage

import React from 'react';
import useAudioRecorder from 'react-audio-recorder-hook';

function AudioRecorderComponent() {
  const {
    startRecording,
    stopRecording,
    cancelRecording,
    pauseRecording,
    resumeRecording,
    playRecording,
    saveRecording,
    isRecording,
    isPaused,
    recordingDuration,
  } = useAudioRecorder({
    audioConstraints: { echoCancellation: true },
  });

  const handlePlay = async () => {
    const audioUrl = await playRecording();
    if (audioUrl) {
      const audio = new Audio(audioUrl);
      audio.play();
    }
  };

  const handleSave = async () => {
    const recording = await saveRecording();
    if (recording) {
      console.log('Recording saved:', recording.blob);
      // You can upload the blob or use it as needed
    }
  };

  return (
    <div>
      <div>
        Recording: {isRecording ? 'Yes' : 'No'}
        {isPaused && ' (Paused)'}
      </div>
      <div>Duration: {recordingDuration}s</div>

      <div>
        {!isRecording && <button onClick={startRecording}>Start Recording</button>}

        {isRecording && !isPaused && <button onClick={pauseRecording}>Pause</button>}

        {isRecording && isPaused && <button onClick={resumeRecording}>Resume</button>}

        {isRecording && <button onClick={stopRecording}>Stop</button>}

        {isRecording && <button onClick={cancelRecording}>Cancel</button>}

        <button onClick={handlePlay}>Play</button>
        <button onClick={handleSave}>Save</button>
      </div>
    </div>
  );
}

API Overview

useAudioRecorder(options)

Options

  • audioConstraints: MediaTrackConstraints - Audio constraints to pass to getUserMedia
  • chunkInterval: number - Recording data chunk interval in milliseconds (default: 500)
  • preferredMimeType: string - Custom MIME type to use if supported
  • onNotSupported: () => void - Called when recording is unsupported on the browser

Returns

An object with the following properties:

  • startRecording: () => Promise - Starts recording audio
  • stopRecording: () => Promise - Stops the current recording
  • cancelRecording: () => void - Cancels the current recording
  • pauseRecording: () => void - Pauses the current recording
  • resumeRecording: () => Promise - Resumes a paused recording
  • saveRecording: () => Promise<{ blob: Blob; url: string } | null> - Creates a blob and URL for the recording
  • playRecording: () => Promise<string | null> - Creates a URL for the recording that can be used with Audio
  • isRecording: boolean - Whether recording is in progress
  • isPaused: boolean - Whether recording is paused
  • recordingDuration: number - Current recording duration in seconds
  • mediaStream: MediaStream | null - The current media stream

Advanced Usage Examples

Audio Recorder with Visualization

This package includes an example component that demonstrates how to implement audio waveform visualization with the hook:

import { AudioRecorderWithVisualization } from 'react-audio-recorder-hook/examples';

function App() {
  return (
    <div className="app">
      <h1>Audio Recorder with Visualization</h1>
      <AudioRecorderWithVisualization
        width={500}
        height={100}
        backgroundColor="#f0f0f0"
        barColor="#3a86ff"
      />
    </div>
  );
}

Testing

The package includes a comprehensive test suite that ensures all functionality works as expected. All tests are now passing.

# Run the test suite
pnpm test

# Generate test coverage report
pnpm test:coverage

Contributing

We welcome contributions! Please see our Contributing Guide for details on how to submit pull requests, report issues, and suggest enhancements.

Roadmap

See our improvements document for planned enhancements and future features.

License

MIT

Troubleshooting

iOS Compatibility

Version 1.0.5 and later includes improved compatibility with iOS devices by automatically detecting iOS browsers and using compatible audio formats (mp4/aac). If you experience issues with audio recording on iOS:

  1. Ensure you're using the latest version of the package
  2. You can explicitly set a compatible MIME type using the preferredMimeType option:
    useAudioRecorder({
      preferredMimeType: 'audio/mp4',
    });
  3. For iOS Safari, ensure your app has proper microphone permissions and is served over HTTPS

If you still encounter issues on iOS devices, please report them on our GitHub issues page.