JavaScript MediaStream getAudioTracks() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

MediaStream.getAudioTracks() returns an array of every MediaStreamTrack in the stream whose kind is "audio". Per MDN, the array is empty when there are no audio tracks.

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

Syntax

Call stream.getAudioTracks. Start with this line.

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

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

Library declarations
JavaScript
mediaStream.getAudioTracks()

Parameters

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

Return Value

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

JavaScript Examples

Example 1 — Basic getAudioTracks()

List audio tracks from a Web Audio destination stream (no mic permission).

JavaScript
const ctx = new AudioContext();
const stream = ctx.createMediaStreamDestination().stream;
const audio = stream.getAudioTracks();

console.log(audio.length); // 1
console.log(audio[0].kind); // "audio"

ctx.close();
Try It Yourself

How It Works

  1. Create a Web Audio destination. Its .stream has one audio track.
  2. Call getAudioTracks(). Returns an array of audio tracks.
  3. Check length. Here it is 1.
  4. Check kind. Always "audio" for these entries.

Example 2 — Video-Only Returns []

Canvas capture has video tracks only — audio list is empty.

JavaScript
const stream = document.createElement("canvas").captureStream();
const audio = stream.getAudioTracks();
const video = stream.getVideoTracks();

console.log(audio.length); // 0
console.log(video.length); // 1
Try It Yourself

How It Works

  1. Capture a canvas stream. One video track, no audio.
  2. Ask for audio tracks. Filter finds none.
  3. Length is 0. Safe empty array — not an error.
  4. Video still present. Use getVideoTracks() for that side.

Example 3 — Audio + Video Stream

Compose both kinds, then split with the getters.

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]);

console.log(stream.getAudioTracks().length); // 1
console.log(stream.getVideoTracks().length); // 1
console.log(stream.getTracks().length);      // 2

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. new MediaStream([audio, video]).
  4. Split by kind. Audio and video getters each report 1; total is 2.

Example 4 — vs getTracks() Filter

Same count as filtering kind === "audio" yourself.

JavaScript
const ctx = new AudioContext();
const stream = ctx.createMediaStreamDestination().stream;

const direct = stream.getAudioTracks();
const filtered = stream.getTracks().filter(
  (t) => t.kind === "audio"
);

console.log(direct.length);   // 1
console.log(filtered.length); // 1
console.log(direct[0] === filtered[0]); // true

ctx.close();
Try It Yourself

How It Works

  1. Get audio the short way. getAudioTracks().
  2. Get audio the long way. Filter getTracks() by kind.
  3. Lengths match. Same selection rule.
  4. Same objects. Prefer the dedicated helper for clarity.

Example 5 — Mute Audio Tracks

Set enabled = false on every audio track (soft mute).

JavaScript
const ctx = new AudioContext();
const stream = ctx.createMediaStreamDestination().stream;

stream.getAudioTracks().forEach((track) => {
  track.enabled = false;
});

console.log(stream.getAudioTracks()[0].enabled); // false

ctx.close();
Try It Yourself

How It Works

  1. List audio tracks. Loop with forEach.
  2. Set enabled = false. Soft mute — track stays in the stream.
  3. Read back. enabled is now false.
  4. vs stop(). Stopping ends the track; muting can be reversed with enabled = true.

Browser Support

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

Supported across modern Chrome, Firefox, Safari, and Edge for listing audio tracks.

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
getAudioTracks() Excellent

Bottom line: Browser only — not a Node.js API. Safe for production. Use it whenever you need only the audio tracks from a MediaStream.

Frequently Asked Questions

An array of MediaStreamTrack objects whose kind is "audio". If the stream has no audio tracks, the array is empty.
No. MDN marks MediaStream.getAudioTracks() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard. Early AudioStreamTrack was merged into MediaStreamTrack.
No. Call it on a MediaStream with no arguments: stream.getAudioTracks().
No. MDN says the order is not defined by the specification and may change from one call to the next.
getTracks() returns every track. getAudioTracks() returns only tracks where track.kind === "audio".
getAudioTracks() returns []. That is normal — check length before using tracks[0].

Next: getTrackById()

Look up a single track by its unique id string.

getTrackById() →

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