The addTrack() method of MediaStreamadds a MediaStreamTrack to the stream. Learn the empty-stream pattern with new MediaStream(), building composed streams track by track, MDN’s duplicate-track rule, how active updates, and comparison with the constructor—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
undefined
03
Parameter
MediaStreamTrack
04
Duplicate
no-op
05
Use case
Compose
06
Status
Baseline widely
Fundamentals
Introduction
A MediaStream is a collection of tracks—audio, video, or both. You often get a full stream from getUserMedia() or captureStream(). When you need to assemble a stream yourself, start with new MediaStream() and call stream.addTrack(track) for each track you want.
MDN: the track argument is a MediaStreamTrack. If that track is already in the stream, addTrack() does nothing. The method returns undefined.
💡
Beginner tip
Think of addTrack() like adding a song to a playlist. The same track object can exist in multiple streams—adding it shares the reference, it does not copy the media source.
Concept
Understanding MediaStream.addTrack()
An instance method that inserts one MediaStreamTrack into the stream’s track set.
Parameter — track: a MediaStreamTrack to add.
Return value — none (undefined).
Duplicate — if the track is already present, no effect (MDN).
MediaStream.addTrack() 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.addTrack()
Add MediaStreamTrack objects to a stream—undefined return, duplicate no-op.
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.addTrack
Not supported
MediaStream.addTrack()Excellent
Bottom line: Use addTrack() to compose streams incrementally from individual tracks.
Wrap Up
Conclusion
MediaStream.addTrack() adds a MediaStreamTrack to a stream and returns undefined. Start with new MediaStream(), add tracks as you need them, and remember MDN’s rule: duplicate adds are ignored.
Use addTrack() when tracks arrive at different times
Check getTracks().length after composing
Pair with removeTrack() for dynamic layouts
Reuse live tracks from getUserMedia() when merging
Verify stream.active after adding live tracks
❌ Don’t
Expect a return value for chaining
Assume duplicate addTrack() increases the count
Forget tracks are shared across streams
Add ended tracks expecting them to become live again
Skip removeTrack() cleanup when replacing tracks
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about addTrack()
Add tracks to compose MediaStream objects.
5
Core concepts
⚙️01
addTrack
1 track
API
🔗02
Returns
undefined
Output
⚠️03
Duplicate
no-op
MDN
📄04
Compose
incremental
Pattern
🎯05
Baseline
since Sep 2017
Status
❓ Frequently Asked Questions
addTrack(track) adds a MediaStreamTrack to the stream. The track parameter must be a MediaStreamTrack object. The method returns undefined.
No. MDN marks MediaStream.addTrack() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
MDN says if the track is already in the stream's track set, addTrack() has no effect—the track is not added again.
No. It returns undefined. Use getTracks() or getVideoTracks() after adding to inspect the stream.
new MediaStream([track]) builds a stream in one step. addTrack() lets you start with new MediaStream() and add tracks incrementally—useful when tracks arrive at different times.
If the added track has readyState live, the stream usually becomes active (see MediaStream.active). An empty stream is inactive until a live track is added.
Did you know?
MDN notes that if the track you pass to addTrack() is already in the stream’s track set, the method has no effect—calling it again does not create a duplicate entry.