JavaScript MediaStream getAudioTracks() Method

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

What You’ll Learn

The getAudioTracks() method of MediaStream returns an array of audio MediaStreamTrack objects in the stream. Learn the MDN getUserMedia timer example that stops the mic, empty arrays for video-only streams, filtering with track.kind, and comparison with getTracks()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Track array

03

Filter

kind audio

04

Empty

no mic tracks

05

Use case

Stop mic

06

Status

Baseline widely

Introduction

A MediaStream can hold both audio and video tracks. When you call getUserMedia({ audio: true, video: true }), the browser usually adds at least one microphone track and one camera track. getAudioTracks() gives you only the audio ones—handy for muting, stopping, or inspecting the mic without touching video.

MDN: the return value is an array where each entry is a MediaStreamTrack with kind === "audio". If there are no audio tracks, you get an empty array []. The order of tracks is not guaranteed and may change between calls.

💡
Beginner tip

Use getAudioTracks()[0] when you expect one microphone—but always check .length first so you do not call .stop() on undefined.

Understanding MediaStream.getAudioTracks()

An instance method that filters the stream’s track set to audio tracks only.

  • Parameters — none.
  • Return value — array of MediaStreamTrack with kind "audio".
  • Empty array — when the stream has no audio tracks (e.g. canvas video only).
  • Order — not defined by the spec; may vary between calls (MDN).
  • Pair withgetVideoTracks() for camera/screen video tracks.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

JavaScript
getAudioTracks()

Parameters

None.

Return value

An array of MediaStreamTrack objects for audio tracks. Empty if none exist.

Typical pattern (MDN idea)

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

⚡ Quick Reference

GoalCode / note
List audio tracksstream.getAudioTracks()
Count audio tracksstream.getAudioTracks().length
First mic trackstream.getAudioTracks()[0]
Stop microphonegetAudioTracks().forEach((t) => t.stop())
Mute (keep track)track.enabled = false
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.getAudioTracks().

Returns
array

Audio tracks

Filter
kind audio

Built-in

Empty
[]

No audio

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream: getAudioTracks(). Mic examples use getUserMedia (secure context + permission); video-only demos use canvas.captureStream().

📚 Getting Started

MDN pattern: stop the first audio track after a delay.

Example 1 — MDN: Stop Audio After 5 Seconds

Get camera + mic, then getAudioTracks()[0].stop() on a timer.

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

How It Works

MDN: getAudioTracks() isolates mic tracks so you can stop audio without ending video.

Example 2 — Audio-Only getUserMedia

Request only audio: true—typically one audio track.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: false })
  .then((stream) => {
    const audioTracks = stream.getAudioTracks();
    console.log(audioTracks.length, audioTracks[0].kind);
  });
Try It Yourself

How It Works

Every returned track has kind "audio" by definition.

📈 Empty Arrays & Comparisons

Video-only streams and filtering alternatives.

Example 3 — Video-Only Stream Returns []

canvas.captureStream() has video tracks only—no audio.

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

const audio = stream.getAudioTracks();
const video = stream.getVideoTracks();

console.log(audio.length, video.length);
Try It Yourself

How It Works

MDN: empty array when the stream contains no audio tracks.

Example 4 — getAudioTracks() vs getTracks().filter()

Both select audio tracks; the dedicated method is clearer.

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

How It Works

Prefer getAudioTracks() for readability; counts should match for standard captures.

Example 5 — Loop Every Audio Track and Mute

Disable all mic tracks without calling stop().

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

navigator.mediaDevices
  .getUserMedia({ audio: true })
  .then((stream) => {
    muteAudio(stream);
    console.log(stream.getAudioTracks()[0].enabled);
  });
Try It Yourself

How It Works

enabled = false mutes output; stop() permanently ends the track.

🚀 Common Use Cases

  • Stop the microphone while keeping the camera running (MDN timer pattern).
  • Build a mute button that toggles track.enabled on audio tracks.
  • Count how many audio inputs are in a composed stream.
  • Route only audio tracks to an analyser or recorder.
  • Verify a stream has audio before attaching to Web Audio API.

🔧 How It Works

1

Get a MediaStream

From getUserMedia, captureStream, WebRTC, or addTrack composition.

Context
2

Call getAudioTracks()

Browser collects tracks where MediaStreamTrack.kind is "audio".

Filter
3

Receive array

Zero or more MediaStreamTrack objects—order not guaranteed by spec.

Result
4

Act on tracks

Stop, mute, or inspect labels—without affecting video tracks.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Order not defined — do not rely on [0] being the same track every call without checking id or label.
  • Secure contextgetUserMedia examples need HTTPS or localhost.
  • Empty array — normal for video-only or empty streams.
  • Related learning: getVideoTracks(), addTrack(), active, getUserMedia().

Universal Browser Support

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

Returns an array of audio MediaStreamTrack objects 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.getAudioTracks
Not supported
MediaStream.getAudioTracks() Excellent

Bottom line: Call getAudioTracks() to list, mute, or stop microphone tracks in a stream.

Conclusion

MediaStream.getAudioTracks() returns every audio track in a stream as an array. Use it to stop or mute the microphone, count audio inputs, or separate audio from video—MDN’s timer example stops tracks[0] after five seconds while video can continue.

Continue with getVideoTracks(), clone(), getUserMedia(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check .length before using [0]
  • Use getAudioTracks() instead of manual filtering
  • Mute with enabled = false for temporary silence
  • Call stop() on audio tracks when done with the mic
  • Pair with getVideoTracks() for full stream control

❌ Don’t

  • Assume track order stays the same between calls
  • Call tracks[0].stop() without checking length
  • Expect audio from canvas.captureStream()
  • Confuse enabled (mute) with stop() (end)
  • Forget secure-context requirements for getUserMedia

Key Takeaways

Knowledge Unlocked

Five things to remember about getAudioTracks()

List and control microphone tracks in a stream.

5
Core concepts
🔗02

Filter

kind audio

Rule
📦03

Empty

[] ok

Edge
📄04

MDN

stop mic

Pattern
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

An array of MediaStreamTrack objects whose kind property 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.
No. MDN documents getAudioTracks() with no parameters. Call it on a MediaStream: 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 in the stream. getAudioTracks() returns only tracks where track.kind === "audio".
After getUserMedia with audio and video, call getAudioTracks()[0].stop() to stop the microphone track—for example after a timer expires.
Did you know?

MDN’s getAudioTracks() example stops only the microphone after five seconds with tracks[0].stop()—the video track can keep running for preview or recording.

Find tracks by id

Learn getTrackById()—look up one MediaStreamTrack by id.

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.

5 people found this page helpful