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
Fundamentals
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.
Concept
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 as — getUserMedia(), which requests hardware and user permission.
Baseline Widely available on MDN (since September 2017).
Foundation
📝 Syntax
JavaScript
new MediaStream()
new MediaStream(stream)
new MediaStream(tracks)
Parameters
Parameter
Meaning
(none)
Empty stream with no tracks
stream
Another MediaStream; its tracks are added (shared, not moved)
tracks
Array 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
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNo MediaStream constructor
Not supported
MediaStream()Excellent
Bottom line: Use new MediaStream() to build or clone track collections in JavaScript.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about MediaStream()
Build and clone media streams in code.
5
Core concepts
⚙️01
new MediaStream
creates object
API
🔗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.