JavaScript MediaStream getTracks() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

MediaStream.getTracks() returns an array of every MediaStreamTrack in the stream’s track set — audio and video alike. Per MDN, kind does not matter; you get the full list.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call stream.getTracks. Start with this line.

JavaScript
mediaStream.getTracks()
CallWhat it does
mediaStream.getTracks()Primary signature.
stream.getTracks(...)Call with the arguments your case needs.

Built into the browser. Call getTracks(...) on a MediaStream — no import required.

Library declarations
JavaScript
mediaStream.getTracks()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Array of all MediaStreamTracks (may be empty)

JavaScript Examples

Example 1 — Basic getTracks()

List every track from a canvas capture stream.

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

console.log(tracks.length); // 1
console.log(tracks[0].kind); // "video"
Try It Yourself

How It Works

  1. Capture a stream. Canvas gives one video track.
  2. Call getTracks(). Returns an array of all tracks.
  3. Check length. Here it is 1.
  4. Check kind. The only track is "video".

Example 2 — Empty Stream Returns []

No tracks yet — the array is empty, not an error.

JavaScript
const stream = new MediaStream();
const tracks = stream.getTracks();

console.log(tracks.length); // 0
console.log(Array.isArray(tracks)); // true
Try It Yourself

How It Works

  1. Create an empty stream. No tracks added yet.
  2. Call getTracks(). Track set is empty.
  3. Length is 0. Safe empty array.
  4. Still an array. You can forEach without extra checks.

Example 3 — Audio + Video Together

Compose two kinds; getTracks() returns both.

JavaScript
const ctx = new AudioContext();
const audioTrack = ctx.createMediaStreamDestination()
  .stream.getAudioTracks()[0];
const videoTrack = document.createElement("canvas")
  .captureStream().getVideoTracks()[0];

const stream = new MediaStream([audioTrack, videoTrack]);
const kinds = stream.getTracks().map((t) => t.kind);

console.log(stream.getTracks().length); // 2
console.log(kinds.join(",")); // "audio,video"

ctx.close();
Try It Yourself

How It Works

  1. Take one audio track. From a Web Audio destination.
  2. Take one video track. From canvas capture.
  3. Build one stream. Both tracks in the set.
  4. List all. Length 2; kinds cover audio and video.

Example 4 — vs Audio / Video Helpers

Full list length equals audio count + video count.

JavaScript
const ctx = new AudioContext();
const stream = new MediaStream([
  ctx.createMediaStreamDestination().stream.getAudioTracks()[0],
  document.createElement("canvas").captureStream().getVideoTracks()[0]
]);

const all = stream.getTracks().length;
const audio = stream.getAudioTracks().length;
const video = stream.getVideoTracks().length;

console.log(all);         // 2
console.log(audio + video); // 2

ctx.close();
Try It Yourself

How It Works

  1. Compose both kinds. One audio + one video.
  2. Count with getTracks(). Full set size.
  3. Count with helpers. Audio and video separately.
  4. Sums match. Helpers partition the same track set.

Example 5 — Stop All Tracks

MDN-style cleanup: stop every track in one loop.

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

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

console.log(stream.getTracks()[0].readyState); // "ended"
console.log(stream.active); // false
Try It Yourself

How It Works

  1. List every track. getTracks() for cleanup.
  2. Call stop() on each. Releases the media source.
  3. Track ends. readyState becomes "ended".
  4. Stream goes inactive. No live tracks left.

Browser Support

MediaStream.getTracks() is Baseline Widely available (MDN: since September 2017). It is part of the Media Capture and Streams specification. It is a browser Web API — not available in Node.js, Bun, or Deno.

✓ Baseline · Widely available

MediaStream.getTracks()

Supported across modern Chrome, Firefox, Safari, and Edge for listing every track in a stream.

98% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
getTracks() Excellent

Bottom line: Browser only — not a Node.js API. Safe for production. Use it whenever you need the full track list or to stop every track.

Frequently Asked Questions

An array of every MediaStreamTrack in the stream — both audio and video — regardless of track.kind. Empty streams return [].
No. MDN marks MediaStream.getTracks() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
No. Call it on a MediaStream with no arguments: stream.getTracks().
getTracks() returns all tracks. getAudioTracks() and getVideoTracks() return only audio or only video tracks.
Call stream.getTracks().forEach((track) => track.stop()) to release camera, microphone, and any other tracks at once.
No. It is a snapshot of the track set at call time. Adding or removing tracks later does not update an array you already stored.

Next: getVideoTracks()

Filter a stream down to its video tracks only.

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.

10 people found this page helpful