Skip to content
tezvyn:

MediaRecorder API: Capture Audio and Video in the Browser

Source: developer.mozilla.orgMediumHow cards are made

MediaRecorder API: Capture Audio and Video in the Browser

The MediaRecorder API is a VCR for your browser, capturing a MediaStream from a camera or screen into a file. Use it for in-browser video recorders or voice memo apps. The footgun: recorded data arrives as Blob objects in an event, not as a direct.

Why it exists

Before this API, recording audio or video in a browser required server-side processing or plugins like Flash. The MediaRecorder API provides a standardized, built-in way to capture media streams directly on the client-side, simplifying development and improving performance for features like in-app video feedback or voice notes.

The mental model

Think of MediaRecorder as a tape recorder for a MediaStream. You give it a live feed (from a camera, mic, or screen), tell it to start(), and it begins capturing that feed. As it records, it periodically hands you "chunks" of the tape (as Blob objects) through an event. When you stop(), it gives you the final piece, and you are responsible for stitching all the chunks together to create the final recording.

How it works

First, you get a MediaStream, typically from navigator.mediaDevices.getUserMedia() or getDisplayMedia(). You instantiate a recorder with new MediaRecorder(stream, { mimeType: 'video/webm' }). To begin, call recorder.start(). To receive data, you must add an event listener for dataavailable. This event fires with a chunk of data. You collect these event.data Blobs in an array. When recorder.stop() is called, a final dataavailable event fires. You then create a new Blob from your array of chunks to form the complete recording.

When to use it

Use the MediaRecorder API for any feature that requires capturing media on the client. This includes user-generated video testimonials, audio messaging, "record your screen" bug reporting tools, or simple web-based video recorders. It's ideal when you want to process or preview media in the browser before uploading.

When not to use it

This API is for recording, not for building a production-grade live streaming service; WebRTC is better suited for true real-time communication. It is also not a video editor; it only captures the raw stream. For complex transcoding or media manipulation, you will need other libraries or server-side tools.

One canonical example

To build a screen recorder, you get a stream via navigator.mediaDevices.getDisplayMedia(). You create new MediaRecorder(stream) and an empty array, recordedChunks. An event listener for dataavailable pushes event.data into that array. A "Start" button calls recorder.start(), and a "Stop" button calls recorder.stop(). After stopping, you create a final blob: const blob = new Blob(recordedChunks, { type: 'video/webm' }), generate a URL with URL.createObjectURL(blob), and set it as the src of a <video> element for playback.

Interview question

How does the MediaRecorder API provide the captured media data to the application?

  • a.It returns a final Blob object immediately upon calling stop().
  • b.It emits multiple dataavailable events, each with a Blob chunk, requiring manual collection.Correct
  • c.It streams the complete recording directly to a server endpoint.
  • d.It saves the recording to a local file system path specified by the developer.
Why?

The MediaRecorder API delivers recorded data as periodic Blob chunks via 'dataavailable' events, which developers must collect and combine. It does not return a single Blob directly after stopping, nor does it handle server streaming or direct local file saving.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles