JavaScript MediaStream addTrack() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The addTrack() method of MediaStream adds 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

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.

Understanding MediaStream.addTrack()

An instance method that inserts one MediaStreamTrack into the stream’s track set.

  • Parametertrack: a MediaStreamTrack to add.
  • Return value — none (undefined).
  • Duplicate — if the track is already present, no effect (MDN).
  • Pair withremoveTrack() to remove a track.
  • Alternativenew MediaStream([trackA, trackB]) in one step.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
addTrack(track)

Parameters

ParameterMeaning
trackA MediaStreamTrack to add to this stream

Return value

None (undefined).

Typical pattern

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

composed.addTrack(videoTrack);
console.log(composed.getTracks().length);

⚡ Quick Reference

GoalCode / note
Add a trackstream.addTrack(track)
Start emptyconst s = new MediaStream()
Count tracksstream.getTracks().length
Duplicate addSecond call has no effect (MDN)
Return valueundefined
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.addTrack().

Adds
1 track

Per call

Returns
undefined

No chaining

Duplicate
no-op

MDN rule

Baseline
widely

Since Sep 2017

Examples Gallery

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

📚 Getting Started

Build a stream from an empty MediaStream.

Example 1 — Empty Stream + addTrack()

Start with zero tracks, then add a canvas video track.

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

const stream = new MediaStream();
stream.addTrack(videoTrack);

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

How It Works

new MediaStream() gives an empty container; addTrack() inserts the track reference.

Example 2 — Add Multiple Tracks from Different Sources

Compose a stream from tracks taken from two canvas captures.

JavaScript
const a = document.createElement("canvas");
const b = document.createElement("canvas");
const trackA = a.captureStream().getVideoTracks()[0];
const trackB = b.captureStream().getVideoTracks()[0];

const composed = new MediaStream();
composed.addTrack(trackA);
composed.addTrack(trackB);

console.log(composed.getTracks().length);
Try It Yourself

How It Works

Each addTrack() call adds one more entry to the stream’s track set.

📈 Edge Cases & Comparisons

Duplicate tracks, active, and constructor alternatives.

Example 3 — Adding the Same Track Twice (MDN No-Op)

MDN: if the track is already in the set, the second addTrack() has no effect.

JavaScript
const canvas = document.createElement("canvas");
const track = canvas.captureStream().getVideoTracks()[0];
const stream = new MediaStream();

stream.addTrack(track);
stream.addTrack(track);

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

How It Works

The track set is unique—duplicate references are ignored.

Example 4 — active After addTrack()

An empty stream is inactive; adding a live track usually makes it active.

JavaScript
const canvas = document.createElement("canvas");
const track = canvas.captureStream().getVideoTracks()[0];
const stream = new MediaStream();

console.log("before:", stream.active);
stream.addTrack(track);
console.log("after:", stream.active);
Try It Yourself

How It Works

See MediaStream.active—true when at least one track is not ended.

Example 5 — addTrack() vs new MediaStream([track])

Two ways to build the same one-track stream.

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

const viaAdd = new MediaStream();
viaAdd.addTrack(track);

const viaCtor = new MediaStream([track]);

console.log(viaAdd.getTracks().length, viaCtor.getTracks().length);
console.log(viaAdd.getTracks()[0] === viaCtor.getTracks()[0]);
Try It Yourself

How It Works

Use addTrack() when tracks arrive over time; use the constructor when you have them all at once.

🚀 Common Use Cases

  • Merge separate audio and video captures into one MediaStream.
  • Add a screen-share track to an existing camera stream in WebRTC.
  • Build streams dynamically as tracks become available.
  • Re-wrap tracks after processing without stopping the source track.
  • Test media pipelines with new MediaStream() plus manual tracks.

🔧 How It Works

1

Get a MediaStreamTrack

From getUserMedia, captureStream, or another stream's getTracks().

Track
2

Call addTrack(track)

Inserts the track if not already in the stream—returns undefined.

Method
3

Stream updates

getTracks() reflects the new count; active may become true for live tracks.

State
4

Use the stream

Attach to video, send over RTCPeerConnection, or record with MediaRecorder.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Duplicate tracks — adding the same track again has no effect.
  • Shared references — the track object may belong to multiple streams.
  • Return value — always undefined; inspect with getTracks().
  • Related learning: MediaStream(), removeTrack(), active, getUserMedia().

Universal Browser Support

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.

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.addTrack
Not supported
MediaStream.addTrack() Excellent

Bottom line: Use addTrack() to compose streams incrementally from individual tracks.

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.

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

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about addTrack()

Add tracks to compose MediaStream objects.

5
Core concepts
🔗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.

Duplicate a stream

Learn MediaStream.clone()—new id and independent track copies.

clone() →

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