# MediaStream

> The `MediaStream` JS object represents a stream of media content made of audio and video tracks.

## What is a MediaStream in JavaScript?

The object that holds one or more audio and video tracks, for example from a microphone, camera or a remote peer. It can be played in a `<video>` element, recorded or sent over WebRTC.

- Topic: Media & Audio
- Type: object
- Browser support: Chrome 55+, Chrome Android 55+, Edge 12+, Firefox 15+, Firefox Android 15+, Safari 11+, Safari iOS 11+

## Example

Wrong:

```js
video.src = URL.createObjectURL(stream);
video.play();
```

Right:

```js
video.srcObject = stream;
await video.play();
```

Streams attach through `srcObject`; passing a `MediaStream` to `createObjectURL()` is no longer supported.

## Common mistakes

- Removing the `<video>` element but leaving the camera or microphone running.

## Key points

- Every track is stopped with `track.stop()` when the stream is no longer needed, so the camera light turns off.
- Remote streams are attached with `srcObject`, not by creating object URLs.

## Interview questions

### How do I turn off the camera in JavaScript?

Call `stream.getTracks().forEach((t) => t.stop())`. The camera light turns off once every video track is stopped.

### How do I mute a MediaStream without stopping it?

Set `track.enabled = false` on the audio track. The track keeps running but carries silence.

### Can I combine tracks into one MediaStream?

Yes. Pass them to the constructor, as in `new MediaStream([videoTrack, audioTrack])`.

## Related cards

- [getUserMedia()](https://wonderium.dev/js/get-user-media.md)
- [RTCPeerConnection](https://wonderium.dev/js/rtc-peer-connection.md)
- [MediaRecorder](https://wonderium.dev/js/media-recorder.md)

---

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