The React Voice Visualizer library provides a simple way to record, visualize, and play back audio in your React applications. This guide will walk you through the essential features and common workflows.
Here’s a complete working example showing how to implement audio recording, playback, and visualization:
import { useEffect } from "react";import { useVoiceVisualizer, VoiceVisualizer } from "react-voice-visualizer";const App = () => { // Initialize the recorder controls using the hook const recorderControls = useVoiceVisualizer(); const { recordedBlob, error, } = recorderControls; // Get the recorded audio blob useEffect(() => { if (!recordedBlob) return; console.log(recordedBlob); // You can upload the blob to your server here // Or convert it to a different format }, [recordedBlob]); // Handle errors useEffect(() => { if (!error) return; console.error(error); }, [error]); return ( <VoiceVisualizer controls={recorderControls} /> );};export default App;
During recording, you can pause and resume using the same toggle function:
const { togglePauseResume, isPausedRecording } = useVoiceVisualizer();// Toggle between pause and resumetogglePauseResume();// Check if recording is pausedconsole.log(isPausedRecording); // true or false
3
Stop Recording
When finished, stop the recording to finalize the audio:
The hook provides pre-formatted time strings for display:
const { formattedRecordingTime, // "09:51" - Current recording time formattedDuration, // "09:51m" - Total duration of recorded audio formattedRecordedAudioCurrentTime, // "09:51:1" - Current playback position} = useVoiceVisualizer();
Use these formatted values directly in your UI to display time information without additional formatting logic.
The hook provides a convenience method to download the recorded audio:
const { saveAudioFile } = useVoiceVisualizer();// Trigger download of recorded audio as .webm filesaveAudioFile();
The saveAudioFile function only supports WebM format. To save in other formats (MP3, WAV, etc.), you’ll need to use external libraries like FFmpeg to convert the recordedBlob to your desired format.
const { clearCanvas, isCleared } = useVoiceVisualizer();// Clear all audio data and reset stateclearCanvas();// Check if canvas is clearedconsole.log(isCleared); // true or false