useVoiceVisualizer hook provides comprehensive controls and state management for audio recording and visualization. It handles microphone access, audio recording, playback, and provides formatted time values for display.
Import
Basic Usage
Parameters
All parameters are optional and can be passed as an object to the hook.Callback function triggered when recording starts.
Callback function triggered when recording stops.
Callback function triggered when recording is paused.
Callback function triggered when recording is resumed.
Callback function triggered when the canvas is cleared.
Callback function triggered when audio playback ends.
Callback function triggered when audio playback starts.
Callback function triggered when audio playback is paused.
Callback function triggered when audio playback is resumed.
Callback function invoked when an error occurs during the execution of
audio.play(). It provides an opportunity to handle and respond to such errors.Determines whether the
beforeunload event handler should be added to the window, preventing page unload if necessary.Return Values
The hook returns aControls object containing state values, functions, refs, and formatted values.
State Values
Indicates if audio recording is currently in progress.
Indicates if audio recording is currently paused.
Audio data for real-time visualization during recording.
Elapsed time during recording in milliseconds.
Duration of the recorded audio in seconds.
Current playback time of the recorded audio in seconds.
Source URL of the recorded audio file for playback.
Indicates if recorded audio playback is paused.
Indicates if the recorded audio is being processed and ‘Processing Audio…’ text is shown.
Indicates if the canvas has been cleared.
Indicates whether recorded audio is available and not currently being processed. This return value can be used to check if it’s an appropriate time to work with recorded audio data in your application.
Recorded audio data in Blob format.
Audio buffer from the recorded Blob.
Error object if any error occurred during recording or playback.
Indicates whether audio processing is occurring during a resize event when audio is recorded and a blob is present.
When set to
true, it indicates that the start recording button has been pressed, but either the permission to record has not yet been granted or the recording itself has not yet commenced. This prop serves as a helpful flag to manage the state of the recording process, allowing components to react accordingly to the current stage of recording initiation.This property indicates whether a preloaded audio blob is available for playback or processing.
MediaRecorder instance used for recording audio.
Formatted Values
Formatted duration time in format
09:51m.Formatted recording current time in format
09:51.Formatted recorded audio current time in format
09:51:1.Functions
Function to start audio recording. Requests microphone permission if not already granted.
Function to stop audio recording.
Function to toggle pause/resume during recording and playback of recorded audio.
Function to start/resume playback of recorded audio.
Function to pause playback of recorded audio.
This function allows you to save the recorded audio as a
webm file format. Please note that it supports saving audio only in the webm format. If you need to save the audio in a different format, you can use external libraries like FFmpeg to convert the Blob to your desired format. This flexibility allows you to tailor the output format according to your specific needs.Function to clear the visualization canvas and reset all states.
Internal function to handle current audio time updates during playback.
Users can use this method to set a preloaded audio blob, enabling seamless integration with user inputs, file uploads, etc.
Refs
Reference to the audio element used for playback.
Internal Functions
Internal function to set
isProcessingAudioOnComplete state.Internal function to set
isProcessingOnResize state.Examples
Callback Lifecycle
Custom Controls
Preloading Audio
See Also
- VoiceVisualizer Component - Component that uses these controls
- Types - TypeScript interface definitions
- Custom UI Guide - Building custom UI controls

