Package Exports
- react-audio-recorder-hook
- react-audio-recorder-hook/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-audio-recorder-hook) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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
Installation
npm install react-audio-recorder-hook
# or
yarn add react-audio-recorder-hook
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
useAudioRecorder(options)
Options
audioConstraints
: MediaTrackConstraints - Audio constraints to pass to getUserMediachunkInterval
: number - Recording data chunk interval in milliseconds (default: 500)preferredMimeType
: string - Custom MIME type to use if supportedonNotSupported
: () => 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 recordingpauseRecording
: () => void - Pauses the current recordingresumeRecording
: () => Promise- Resumes a paused recording saveRecording
: () => Promise<{ blob: Blob; url: string } | null> - Creates a blob and URL for the recordingplayRecording
: () => Promise<string | null> - Creates a URL for the recording that can be used with AudioisRecording
: boolean - Whether recording is in progressisPaused
: boolean - Whether recording is pausedrecordingDuration
: number - Current recording duration in secondsmediaStream
: MediaStream | null - The current media stream
License
MIT