# MediaRecorder

> The `MediaRecorder` JS object records audio or video from a `MediaStream`.

## How do I record audio in the browser?

The object that records a `MediaStream` into compressed chunks, for example to upload a voice message or send audio for transcription. Supported formats differ between browsers.

- Topic: Media & Audio
- Type: object
- Browser support: Chrome 47+, Chrome Android 47+, Edge 79+, Firefox 25+, Firefox Android 25+, Safari 14.1+, Safari iOS 14+

## Example

Wrong:

```js
const recorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
recorder.ondataavailable = (e) => upload(e.data);
```

Right:

```js
const type = ['audio/webm', 'audio/mp4'].find((t) => MediaRecorder.isTypeSupported(t));
const recorder = new MediaRecorder(stream, { mimeType: type });
const chunks = [];
recorder.ondataavailable = (e) => chunks.push(e.data);
recorder.onstop = () => upload(new Blob(chunks, { type }));
```

Pick a supported format at runtime and build one `Blob` from all chunks after `stop`.

## Common mistakes

- Hardcoding one audio format that does not work in every browser.

## Key points

- The MIME type is checked with `MediaRecorder.isTypeSupported()` before recording.
- Chunks from `dataavailable` are collected and combined into one `Blob` after `stop`.

## Interview questions

### Why is my MediaRecorder recording empty?

Data arrives in `dataavailable` events. Combine the chunks in the `stop` event handler, not right after calling `stop()`.

### How do I get recorded data while recording is still running?

Pass a timeslice in milliseconds to `start()`, like `recorder.start(1000)`, to get a `dataavailable` event about every second.

### Can MediaRecorder record the screen?

Yes. Pass the stream from `getDisplayMedia()` to `MediaRecorder` like any other `MediaStream`.

## Related cards

- [MediaStream](https://wonderium.dev/js/media-stream.md)
- [getUserMedia()](https://wonderium.dev/js/get-user-media.md)
- [Speech-to-Text](https://wonderium.dev/ai/speech-to-text.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/media-recorder
- Last updated: 2026-11-25
