The MediaRecorder API makes it possible to record audio and video directly in the browser, without Flash, plugins, or server-side processing. It's the foundation of browser-based screen recorders, voice memo apps, video editors, and communication tools. Here's how to use it effectively.
Requesting Permissions
Before recording, you need the user's permission to access their microphone or camera. The browser handles the permission prompt — you just call getUserMedia:
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: { width: 1280, height: 720 }
});
// Stream is ready — start recording
} catch (err) {
if (err.name === 'NotAllowedError') {
console.log('User denied permission');
} else if (err.name === 'NotFoundError') {
console.log('No microphone/camera found');
}
}
Key rules for permissions:
- HTTPS is required. getUserMedia doesn't work on http:// (except localhost for development)
- Request permission only when the user initiates an action (button click), not on page load
- If the user denies permission, you can't re-request — they need to change it in browser settings
- Always handle the rejection case gracefully with a clear message
Creating a MediaRecorder
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=vp9,opus',
videoBitsPerSecond: 2500000, // 2.5 Mbps
audioBitsPerSecond: 128000 // 128 kbps
});
The mimeType specifies the container and codecs. To check what the browser supports:
MediaRecorder.isTypeSupported('video/webm;codecs=vp9');
MediaRecorder.isTypeSupported('video/mp4;codecs=h264,aac');
Chrome and Firefox support WebM with VP9/VP8 and Opus/Vorbis. Safari 14.3+ supports MP4 with H.264 and AAC. For cross-browser compatibility, check supported types and pick the best available:
function pickMimeType() {
const types = [
'video/webm;codecs=vp9,opus',
'video/webm;codecs=vp8,opus',
'video/mp4;codecs=h264,aac',
'video/webm'
];
return types.find(t => MediaRecorder.isTypeSupported(t)) || '';
}
Recording and Saving
const chunks = [];
recorder.ondataavailable = (event) => {
if (event.data.size > 0) {
chunks.push(event.data);
}
};
recorder.onstop = () => {
const blob = new Blob(chunks, { type: recorder.mimeType });
const url = URL.createObjectURL(blob);
// Create a download link
const a = document.createElement('a');
a.href = url;
a.download = `recording.${recorder.mimeType.includes('mp4') ? 'mp4' : 'webm'}`;
a.click();
// Or play back in a video element
videoElement.src = url;
// Clean up the object URL later
// URL.revokeObjectURL(url);
};
// Start recording
recorder.start(1000); // Collect data every 1 second
// Stop recording (e.g., on button click)
// recorder.stop();
Passing a timeslice to start() (e.g., start(1000)) triggers ondataavailable every second, which is useful for long recordings to avoid holding everything in memory.
Recording Specific Sources
Microphone Only
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
Screen Capture
const stream = await navigator.mediaDevices.getDisplayMedia({
video: { cursor: 'always' },
audio: false // Set to true to capture system audio (Chrome only)
});
Screen capture uses getDisplayMedia instead of getUserMedia. The user picks which screen or window to share.
Camera + Screen (Picture-in-Picture)
const screenStream = await navigator.mediaDevices.getDisplayMedia({video: true});
const cameraStream = await navigator.mediaDevices.getUserMedia({video: true});
// Combine: draw both on a canvas, then record the canvas stream
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Draw screen as background, camera as inset
const canvasStream = canvas.captureStream(30);
const combined = new MediaRecorder(canvasStream);
Common Issues and Solutions
- Recording stops after a few seconds: Some browsers stop recording when the tab is backgrounded. Keep the tab active during recording.
- Audio is missing: Ensure the audio track is included in the stream and the mimeType includes an audio codec.
- File won't play in other apps: WebM files from Chrome may not play in QuickTime. Convert to MP4 if you need cross-player compatibility.
- Large file sizes: Lower the bitrate or resolution. For screen recordings of static content, 1 Mbps is often sufficient.
- iOS Safari limitations: MediaRecorder support on iOS is limited. Test thoroughly on actual devices.
Privacy and Security
- Always show a clear indication when recording is active (red dot, timer)
- Stop all tracks when recording ends:
stream.getTracks().forEach(t => t.stop()) - Never upload recordings without explicit user consent
- Store recordings locally by default; only upload if the user explicitly chooses to
The media recorder on this site uses the MediaRecorder API to capture audio and video entirely in your browser. No recordings are uploaded — they stay on your device until you explicitly download or delete them.