JavaScript MediaStream Constructor

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Web API constructor

What You’ll Learn

The MediaStream() constructor returns a newly created MediaStream—a collection of MediaStreamTrack objects. Learn the three MDN forms (new MediaStream(), new MediaStream(stream), new MediaStream(tracks)), how shared tracks work, and when to use the constructor vs getUserMedia()—with five examples and try-it labs.

01

Create

new MediaStream()

02

Empty

no tracks

03

From stream

share tracks

04

From array

track list

05

Returns

MediaStream

06

Status

Baseline widely

Introduction

Most beginners meet MediaStream through navigator.mediaDevices.getUserMedia(), which returns a live camera or microphone stream. The browser can also build streams in JavaScript with new MediaStream()—handy when you compose tracks manually, clone an existing stream, or start with an empty container and add tracks later.

MDN: if you pass parameters, those tracks are added to the new stream. Otherwise the stream has no tracks. When you pass another MediaStream, its tracks are shared—they are not removed from the original stream.

💡
Beginner tip

Think of MediaStream as a playlist of tracks. The constructor either gives you an empty playlist, copies references from another playlist, or starts with the tracks you list.

Understanding the MediaStream() Constructor

A Web API constructor that returns a new MediaStream object—either empty or containing the tracks you provide.

  • No arguments — empty stream, zero tracks.
  • stream — optional; copies track references from another MediaStream (shared).
  • tracks — optional array of MediaStreamTrack objects to add.
  • Return value — a newly created MediaStream (with its own id).
  • Not the same asgetUserMedia(), which requests hardware and user permission.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
new MediaStream()
new MediaStream(stream)
new MediaStream(tracks)

Parameters

ParameterMeaning
(none)Empty stream with no tracks
streamAnother MediaStream; its tracks are added (shared, not moved)
tracksArray of MediaStreamTrack objects to include

Return value

A newly-created MediaStream object.

Empty stream

JavaScript
const empty = new MediaStream();
console.log(empty.getTracks().length); // 0

From track array

JavaScript
const source = canvas.captureStream();
const track = source.getVideoTracks()[0];
const composed = new MediaStream([track]);

From another stream

JavaScript
const original = canvas.captureStream();
const clone = new MediaStream(original);
// original still has its tracks — they are shared

⚡ Quick Reference

GoalCode / note
Empty streamnew MediaStream()
From tracksnew MediaStream([trackA, trackB])
Clone referencesnew MediaStream(existingStream)
Count tracksstream.getTracks().length
Each stream uniqueNew object gets new id
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream().

Creates
MediaStream

New object

Default
empty

0 tracks

Clone
shared

Same tracks

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream: MediaStream(). Labs use canvas.captureStream() so no camera permission is required.

📚 Getting Started

MDN forms: empty stream and track collections.

Example 1 — Empty new MediaStream()

No arguments means zero tracks—a blank container.

JavaScript
const stream = new MediaStream();

console.log(stream instanceof MediaStream);
console.log(stream.getTracks().length);
console.log(stream.active);
Try It Yourself

How It Works

MDN: without parameters the stream has no tracks. active is false when empty.

Example 2 — new MediaStream(tracks) from a Track Array

Pass an array of MediaStreamTrack objects.

JavaScript
const canvas = document.createElement("canvas");
const source = canvas.captureStream();
const videoTrack = source.getVideoTracks()[0];

const stream = new MediaStream([videoTrack]);

console.log(stream.getTracks().length);
console.log(stream.getVideoTracks()[0] === videoTrack);
Try It Yourself

How It Works

Each track in the array is added to the new stream—the same track object, not a copy.

📈 Cloning & Sharing

Build from another stream and understand shared tracks.

Example 3 — new MediaStream(stream) Clone Track References

MDN: tracks are added but not removed from the original.

JavaScript
const canvas = document.createElement("canvas");
const original = canvas.captureStream();
const clone = new MediaStream(original);

console.log(original.getTracks().length);
console.log(clone.getTracks().length);
console.log(original.id === clone.id);
Try It Yourself

How It Works

Both streams reference the same tracks, but each MediaStream object has its own id.

Example 4 — Stopping a Shared Track Affects Both Streams

Because tracks are shared, track.stop() ends them everywhere.

JavaScript
const canvas = document.createElement("canvas");
const a = canvas.captureStream();
const b = new MediaStream(a);

a.getTracks()[0].stop();

console.log(a.active);
console.log(b.active);
Try It Yourself

How It Works

Cloning shares track objects—lifecycle changes apply to every stream that holds that track.

Example 5 — Feature Detect MediaStream

Guard before calling the constructor in older or restricted environments.

JavaScript
const supported = typeof MediaStream === "function";

if (supported) {
  const stream = new MediaStream();
  console.log("ok", stream.getTracks().length);
} else {
  console.log("MediaStream not available");
}
Try It Yourself

How It Works

Modern browsers expose MediaStream globally; always check before use in polyfill scenarios.

🚀 Common Use Cases

  • Start with new MediaStream() and add tracks with addTrack() later.
  • Compose custom streams from selected audio and video tracks.
  • Clone track references for WebRTC or recording pipelines.
  • Unit-test media code without opening real cameras.
  • Split one capture into multiple stream objects for different consumers.

🔧 How It Works

1

Call new MediaStream

No args, a stream, or a track array—pick the MDN overload you need.

Create
2

Browser builds object

Returns a MediaStream with a new id and zero or more track references.

Object
3

Tracks are shared

Cloning from another stream copies references—tracks stay on the source too.

Share
4

Use the stream

Attach to video elements, peer connections, or recorders; inspect with active and id.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Shared tracksnew MediaStream(other) does not move tracks off the source stream.
  • Empty streamactive is false until a live track is added.
  • vs getUserMedia — constructor does not request permission or open hardware by itself.
  • Related learning: MediaStream.active, MediaStream.id, getUserMedia(), addTrack().

Universal Browser Support

MediaStream() is marked Baseline Widely available on MDN (since September 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MediaStream()

Create empty streams or compose them from tracks and other streams.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer No MediaStream constructor
Not supported
MediaStream() Excellent

Bottom line: Use new MediaStream() to build or clone track collections in JavaScript.

Conclusion

MediaStream() creates a new media stream in JavaScript. Call it with no arguments for an empty stream, pass a track array to compose tracks, or pass another stream to share its tracks. Each new object gets its own id; track references may be shared across streams.

Continue with MediaStream.id, MediaStream.active, getUserMedia(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use new MediaStream(tracks) to compose custom feeds
  • Remember cloned streams share track objects
  • Check typeof MediaStream === "function" when needed
  • Pair with addTrack() / removeTrack() for dynamic streams
  • Log stream.id when debugging multiple streams

❌ Don’t

  • Expect new MediaStream(other) to move tracks off the source
  • Confuse the constructor with getUserMedia() permission flow
  • Assume an empty stream can play in <video> without tracks
  • Stop shared tracks unless every stream should end
  • Forget that each new MediaStream() creates a new id

Key Takeaways

Knowledge Unlocked

Five things to remember about MediaStream()

Build and clone media streams in code.

5
Core concepts
🔗02

Empty

0 tracks

Default
📦03

tracks[]

compose

Array
📄04

Clone

shared refs

Share
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

new MediaStream() returns a newly created MediaStream object—a collection of MediaStreamTrack objects. With no arguments the stream is empty; you can also pass another stream or an array of tracks.
No. MDN marks the MediaStream() constructor as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
getUserMedia() asks the user for camera/microphone permission and returns a live stream from hardware. new MediaStream() lets you build or clone streams in code without opening devices—useful for composition and testing.
No. new MediaStream(otherStream) adds references to the same tracks. They are shared by both streams; stopping a track affects every stream that contains it.
MDN expects an array of MediaStreamTrack objects. Each track in the array is added to the new stream.
Yes. Even new MediaStream() with no tracks gets a browser-assigned id string—see the MediaStream.id property tutorial.
Did you know?

When you pass another MediaStream to the constructor, MDN says the tracks are shared by both streams—they are not removed from the original. Stopping a track ends it for every stream that references it.

Add tracks to a stream

Learn MediaStream.addTrack()—compose streams track by track.

addTrack() →

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.

5 people found this page helpful