Browser APIs
useMediaRecorder
About
React hook for audio/video recording using the MediaRecorder API. Provides methods to record, pause, resume, and stop media capture from any MediaStream source.
Installation
Usage
Return Value
Returns an object with the following properties:
| Property | Type | Description |
|---|---|---|
| startRecording | () => void | Start recording |
| stopRecording | () => void | Stop recording |
| pauseRecording | () => void | Pause recording |
| resumeRecording | () => void | Resume recording |
| recordingState | RecordingState | Current recording state |
| data | Blob | null | Recorded data as a Blob |
| dataUrl | string | null | URL to the recorded data |
| error | Error | null | Any error that occurred |
| isSupported | boolean | Whether MediaRecorder API is supported |
Recording States
"idle"- Not recording"recording"- Currently recording"paused"- Recording paused"stopped"- Recording stopped (data available)
MediaRecorder Options
The hook accepts an optional options parameter:
| Option | Type | Description |
|---|---|---|
| mimeType | string | MIME type for the recording |
| audioBitsPerSecond | number | Audio bits per second |
| videoBitsPerSecond | number | Video bits per second |
| bitsPerSecond | number | Overall bits per second |
Examples
Video Recording
Screen Recording
Features
- Audio/video recording - Record from any MediaStream source
- Pause/resume - Control recording with pause and resume
- Multiple sources - Works with microphone, camera, screen capture
- Automatic cleanup - Properly manages resources and memory
- Error handling - Comprehensive error handling
- TypeScript support - Full type definitions with generics
Browser Support
The MediaRecorder API is supported in:
- Chrome 47+
- Firefox 25+
- Safari 14.1+
- Edge 79+
- Opera 36+
Notes
- You must obtain a MediaStream before using the hook (via getUserMedia, getDisplayMedia, etc.)
- The recorded Blob is available after calling
stopRecording() dataUrlis automatically created and can be used for playback or download- Data URL is automatically revoked on unmount to prevent memory leaks
- Supported MIME types vary by browser - check
MediaRecorder.isTypeSupported() - The MediaRecorder is automatically stopped on component unmount