JavaScript MediaStream

Beginner
8 min read
Updated: Nov 2026
3 examples
Media tracks

Definition and Usage

A MediaStream is a stream of media—usually audio and/or video from a camera or microphone. Browsers often create one through navigator.mediaDevices.getUserMedia().

A stream holds one or more MediaStreamTrack objects. Inspect them with getTracks(), add with addTrack(), and remove with removeTrack(). Assign the stream to video.srcObject to show it.

This page teaches what MediaStream is. Constructor, methods, and properties live under this section.

Permission is required.

getUserMedia asks the user for camera or mic access. Without permission the promise rejects.

JavaScript Syntax

Typical MediaStream usage:

JavaScript
const stream = await navigator.mediaDevices.getUserMedia({
  video: true,
  audio: true
});
videoEl.srcObject = stream;
console.log(stream.id);
console.log(stream.getTracks().length);

Core Ideas

Ideas every MediaStream tutorial builds on:

IdeaExampleRemember
StreamMediaStreamBundle of media tracks
getUserMediamediaDevices.getUserMediaCamera / mic access
Tracksstream.getTracks()Audio and video pieces
Showvideo.srcObject = streamPlay in a video element
idstream.idUnique stream id string
activestream.activeWhether it still has live tracks

JavaScript Examples

Three short programs that show how MediaStream works.

Example 1 — Create an empty MediaStream

You can construct an empty stream and add tracks later.

JavaScript
const stream = new MediaStream();
console.log(stream.getTracks().length);
console.log(typeof stream.id);

How It Works

  1. new MediaStream() starts with no tracks.
  2. getTracks() returns an empty array.
  3. id is still a unique string.

Example 2 — List track kinds

After getUserMedia, inspect each track’s kind.

JavaScript
// after getUserMedia:
for (const track of stream.getTracks()) {
  console.log(track.kind);
}

How It Works

  1. getTracks() returns every track on the stream.
  2. kind is usually "audio" or "video".
  3. Stop tracks with track.stop() when finished.

Example 3 — Check whether the stream is active

active is true while at least one track is live.

JavaScript
console.log(stream.active);
stream.getTracks().forEach((t) => t.stop());
console.log(stream.active);

How It Works

  1. active starts true while tracks live.
  2. stop() ends each track.
  3. After all stop, active becomes false.

Next: MediaStream Guides

Continue with the constructor and track methods.

Look upWhy
ConstructorCreate MediaStream objects
addTrack()Attach a track
getTracks()List all tracks
idUnique stream identifier

Full lists: MediaStream constructor.

Frequently Asked Questions

A MediaStream is a collection of media tracks, such as microphone audio and camera video, that you can play in a video element or send over a connection.
Call navigator.mediaDevices.getUserMedia({ video: true }). The user must allow permission. The promise resolves with a MediaStream.
Set videoElement.srcObject = stream. Do not use createObjectURL for this modern path.
Call stop() on each track from stream.getTracks(). That ends capture and usually turns off the indicator.
Open the MediaStream constructor, then Navigator for mediaDevices helpers.

Open the MediaStream constructor

Then explore addTrack, getTracks, and stream properties.

MediaStream constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful