JavaScript MediaStream getTracks() Method

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

What You’ll Learn

The getTracks() method of MediaStream returns an array of every MediaStreamTrack in the stream—audio and video together. Learn the MDN timer example that stops a camera track, counting and logging track.kind, stopping all tracks for cleanup, and comparison with getAudioTracks()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Track array

03

Scope

All kinds

04

Cleanup

stop all

05

Use case

MDN stop

06

Status

Baseline widely

Introduction

A MediaStream is a collection of tracks—microphone, camera, screen share, or synthetic canvas output. When you need to inspect every track at once, call getTracks(). MDN: it returns a sequence representing all tracks in the stream’s track set, regardless of MediaStreamTrack.kind.

This is the go-to method for cleanup: loop the array and call track.stop() to release the camera and mic. It is also the foundation for filtering—many apps use getTracks().filter(...) before the dedicated getAudioTracks() helpers existed.

💡
Beginner tip

When you are done with a stream, always stop every track: stream.getTracks().forEach((t) => t.stop())

Understanding MediaStream.getTracks()

An instance method that returns the full track list for this stream.

  • Parameters — none.
  • Return value — array of MediaStreamTrack objects (audio and video).
  • Kind agnostic — includes every track regardless of kind.
  • Empty array — when the stream has no tracks.
  • Pair withgetAudioTracks(), getVideoTracks(), getTrackById().
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
getTracks()

Parameters

None.

Return value

An array of MediaStreamTrack objects representing every track in the stream.

Typical pattern (MDN idea)

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: false, video: true })
  .then((mediaStream) => {
    document.querySelector("video").srcObject = mediaStream;
    setTimeout(() => {
      const tracks = mediaStream.getTracks();
      tracks[0].stop();
    }, 5000);
  });

⚡ Quick Reference

GoalCode / note
List all tracksstream.getTracks()
Count tracksstream.getTracks().length
Stop every trackgetTracks().forEach((t) => t.stop())
Filter audiogetTracks().filter((t) => t.kind === "audio")
Log kindsgetTracks().map((t) => t.kind)
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.getTracks().

Returns
array

All tracks

Kinds
audio+video

Every kind

Cleanup
.stop()

Loop all

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream: getTracks(). Camera demos use getUserMedia (secure context + permission).

📚 Getting Started

MDN pattern: stop the first track after a delay.

Example 1 — MDN: Stop Video Track After 5 Seconds

Video-only getUserMedia, then getTracks()[0].stop().

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: false, video: true })
  .then((mediaStream) => {
    document.querySelector("video").srcObject = mediaStream;
    setTimeout(() => {
      const tracks = mediaStream.getTracks();
      if (tracks[0]) tracks[0].stop();
    }, 5000);
  });
Try It Yourself

How It Works

MDN: getTracks() returns every track; with video-only input, [0] is the camera.

Example 2 — Log Track Count and Kinds

Audio + video stream—inspect the full track list.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    const tracks = stream.getTracks();
    const kinds = tracks.map((t) => t.kind);
    console.log(tracks.length, kinds.join(", "));
  });
Try It Yourself

How It Works

getTracks() includes both kinds in one array.

📈 Cleanup & Filtering

Stop all tracks and filter by kind.

Example 3 — Stop All Tracks (Cleanup Helper)

Release camera and microphone together when leaving a page.

JavaScript
function stopStream(stream) {
  stream.getTracks().forEach((track) => {
    track.stop();
  });
}

navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    stopStream(stream);
    console.log(stream.getTracks().length);
  });
Try It Yourself

How It Works

Tracks remain in the array after stop(), but are no longer live.

Example 4 — Filter Audio with getTracks()

Manual filter vs dedicated getAudioTracks().

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    const filtered = stream.getTracks().filter((t) => t.kind === "audio");
    const direct = stream.getAudioTracks();
    console.log(filtered.length, direct.length);
  });
Try It Yourself

How It Works

Prefer getAudioTracks() for clarity; getTracks().filter() is equivalent.

Example 5 — Canvas Stream Has One Video Track

canvas.captureStream()—inspect with getTracks().

JavaScript
const canvas = document.createElement("canvas");
const stream = canvas.captureStream();
const tracks = stream.getTracks();

console.log(tracks.length, tracks[0] && tracks[0].kind);
Try It Yourself

How It Works

Synthetic streams still expose tracks through getTracks().

🚀 Common Use Cases

  • Stop every track when the user leaves a call or closes a modal (cleanup).
  • Count how many tracks are in a composed or WebRTC stream.
  • Log track.kind and track.label for debugging.
  • Filter or find tracks before dedicated helper methods.
  • MDN timer pattern: end a single track after a preview period.

🔧 How It Works

1

Get a MediaStream

From getUserMedia, captureStream, WebRTC, or addTrack composition.

Context
2

Call getTracks()

Browser collects every MediaStreamTrack in the stream track set.

Snapshot
3

Receive array

Audio and video tracks together—zero or more entries.

Result
4

Loop or filter

Stop, mute, log, or pass tracks to other APIs.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • All kinds — unlike getAudioTracks(), this includes video tracks too.
  • Cleanup pattern — always stop tracks you no longer need to free camera/mic hardware.
  • Stopped tracks — may still appear in the array; check track.readyState.
  • Related learning: getAudioTracks(), getTrackById(), addTrack(), getUserMedia().

Universal Browser Support

MediaStream.getTracks() 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.getTracks()

Returns an array of every MediaStreamTrack in the stream.

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

Bottom line: Call getTracks() to list, inspect, or stop every track in a MediaStream.

Conclusion

MediaStream.getTracks() returns every track in a stream as an array—audio and video together. Use it to inspect the full track list, filter by kind, or stop all tracks for cleanup. MDN’s example stops tracks[0] after five seconds on a video-only capture.

Continue with getTrackById(), getAudioTracks(), addTrack(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Stop all tracks when done: getTracks().forEach(t => t.stop())
  • Use getTracks() when you need every kind at once
  • Check .length before using [0]
  • Log kind and label for debugging
  • Prefer getAudioTracks() when you only need audio

❌ Don’t

  • Leave camera/mic tracks running after navigation
  • Assume [0] is always video or audio
  • Forget secure-context requirements for getUserMedia
  • Confuse stream id with track id
  • Expect stopped tracks to disappear from the array instantly

Key Takeaways

Knowledge Unlocked

Five things to remember about getTracks()

List and control every track in a stream.

5
Core concepts
🔗02

Kinds

all tracks

Scope
📦03

Cleanup

stop all

Pattern
📄04

MDN

timer stop

Example
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

An array of every MediaStreamTrack in the stream—both audio and video tracks, regardless of track.kind.
No. MDN marks MediaStream.getTracks() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
No. MDN documents getTracks() with no parameters. Call it on a MediaStream: stream.getTracks().
getTracks() returns all tracks. getAudioTracks() and getVideoTracks() return only audio or only video tracks.
After getUserMedia with video only, call getTracks() and tracks[0].stop() on a timer to end the camera track.
Call stream.getTracks().forEach((track) => track.stop()) to release camera, microphone, and any other tracks at once.
Did you know?

MDN’s getTracks() example stops only the first track after five seconds—a simple way to end a video preview without writing separate audio/video logic.

List video tracks

Learn getVideoTracks()—camera tracks in any MediaStream.

getVideoTracks() →

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