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:

Advertisement

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

Privacy and Security

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.