JavaScript MediaStream active Property

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

What You’ll Learn

The read-only active property on a MediaStream tells you whether the stream still has at least one track that has not ended. Learn the MDN getUserMedia pattern, how track.stop() flips active to false, the inactive event, and a permission-free canvas.captureStream() demo—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Boolean

03

Read-only

no setter

04

Rule

any track live

05

Use case

UI state

06

Status

Baseline widely

Introduction

A MediaStream is a bundle of MediaStreamTrack objects—audio, video, or both. You get one from APIs such as getUserMedia(), captureStream() on a canvas, or WebRTC peer connections.

The active property answers a simple question: is this stream still going? It is true while at least one track has a readyState other than "ended". When you stop every track, MDN says active becomes false.

💡
Beginner tip

Think of active as a stream-level light switch. Each track can be "live" or "ended"; the stream stays active until all lights are off.

Understanding the active Property

A read-only instance property on MediaStream that reports whether the stream still has usable tracks.

  • Type — boolean (true or false).
  • Read-only — you cannot assign to stream.active.
  • true — at least one track’s readyState is not "ended".
  • false — every track in the stream has readyState === "ended".
  • Typical source — return value of navigator.mediaDevices.getUserMedia().
  • Related eventinactive fires when active becomes false.
  • Baseline Widely available on MDN (since September 2017).

📝 Syntax

Read active on any MediaStream object:

JavaScript
mediaStream.active

Return value

A boolean: true if the stream has at least one track that is not ended; otherwise false.

Typical pattern (MDN idea)

JavaScript
const startBtn = document.getElementById("start");
const stopBtn = document.getElementById("stop");

startBtn.addEventListener("click", () => {
  navigator.mediaDevices
    .getUserMedia({ audio: true, video: true })
    .then((stream) => {
      startBtn.disabled = stream.active;
      stopBtn.disabled = !stream.active;
    });
});

⚡ Quick Reference

GoalCode / note
Is stream still live?stream.active
Stop everythingstream.getTracks().forEach((t) => t.stop())
Listen for endstream.addEventListener("inactive", handler)
Check one tracktrack.readyState === "live"
Read-onlyCannot assign to active
MDN statusBaseline Widely available (since September 2017)

🔍 At a Glance

Four facts to remember about MediaStream.active.

Type
boolean

true / false

Access
read-only

No setter

Rule
any live

Track not ended

Baseline
widely

Since Sep 2017

Examples Gallery

Examples follow MDN MediaStream.active. Labs use getUserMedia (secure context + permission) or canvas.captureStream() when no camera is needed.

📚 Getting Started

MDN pattern: read active after getUserMedia().

Example 1 — MDN: Disable Start When Stream Is Active

After permission, stream.active is usually true.

JavaScript
const startBtn = document.getElementById("start");
let stream;

startBtn.addEventListener("click", () => {
  navigator.mediaDevices
    .getUserMedia({ audio: true, video: true })
    .then((s) => {
      stream = s;
      console.log("active:", stream.active);
      startBtn.disabled = stream.active;
    });
});
Try It Yourself

How It Works

MDN: when getUserMedia succeeds, live tracks make active true—useful for UI state.

Example 2 — active Becomes false After Stopping All Tracks

Call stop() on every track to end the stream.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true })
  .then((stream) => {
    console.log("before:", stream.active);
    stream.getTracks().forEach((track) => track.stop());
    console.log("after:", stream.active);
  });
Try It Yourself

How It Works

When every track’s readyState is "ended", MDN says active is false.

📈 Tracks, Events & Alternatives

Partial stops, the inactive event, and canvas streams.

Example 3 — Stopping One Track May Leave active true

With audio and video, ending only video often keeps the stream active.

JavaScript
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    const videoTrack = stream.getVideoTracks()[0];
    videoTrack.stop();
    console.log("video ended:", videoTrack.readyState);
    console.log("stream.active:", stream.active);
  });
Try It Yourself

How It Works

active is stream-wide: one live audio track is enough to keep it true.

Example 4 — Listen for the inactive Event

React when the stream fully shuts down.

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

stream.addEventListener("inactive", () => {
  console.log("stream inactive, active =", stream.active);
});

stream.getTracks().forEach((track) => track.stop());
Try It Yourself

How It Works

The inactive event fires on the MediaStream when active becomes false.

Example 5 — canvas.captureStream() Without Camera Permission

Practice active in any secure context—no mic or camera needed.

JavaScript
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.fillRect(0, 0, 40, 40);

const stream = canvas.captureStream();
console.log("created:", stream.active);

stream.getTracks().forEach((track) => track.stop());
console.log("stopped:", stream.active);
Try It Yourself

How It Works

Any MediaStream follows the same active rules—not only camera streams.

🚀 Common Use Cases

  • Disable a “Start camera” button while a stream is already active (MDN pattern).
  • Enable “Stop” or cleanup UI only when stream.active is true.
  • Guard against attaching ended streams to a <video> element.
  • Listen for inactive to release UI or notify the user recording ended.
  • Debug whether tracks were stopped after a call or tab switch.

🔧 How It Works

1

Get a MediaStream

From getUserMedia(), captureStream(), WebRTC, or similar APIs.

Context
2

Tracks start live

Each MediaStreamTrack has readyState "live" while producing media.

Tracks
3

Read stream.active

true if any track is not ended—read-only, no parameters.

Property
4

All tracks end

active becomes false; inactive event fires. Stop tracks with track.stop() or remove them from the stream.

📝 Notes

  • MDN: Baseline Widely available (since September 2017) — no Deprecated / Experimental / Non-standard banner.
  • Read-only — you cannot set stream.active = false; stop the tracks instead.
  • Secure contextgetUserMedia() requires HTTPS or localhost; active itself works on any stream.
  • Stopping one of several tracks may leave active true until every track ends.
  • Related learning: navigator.mediaDevices, getUserMedia(), MediaStreamTrack.readyState.

Universal Browser Support

MediaStream.active 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.active

Read-only boolean—true while any track in the stream is not ended.

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.active
Not supported
MediaStream.active Excellent

Bottom line: Check stream.active before UI actions; stop all tracks to end the stream.

Conclusion

MediaStream.active is a read-only boolean that is true while at least one track is not ended. Use it after getUserMedia() for UI state, pair it with track.stop() for cleanup, and listen for inactive when the stream fully shuts down.

Continue with navigator.mediaDevices, getUserMedia(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check stream.active before starting duplicate captures
  • Stop every track with getTracks().forEach(t => t.stop())
  • Use inactive for cleanup when the stream ends
  • Pair with secure-context checks before getUserMedia
  • Use canvas.captureStream() to learn without hardware

❌ Don’t

  • Try to assign stream.active = false
  • Assume one stopped track means the whole stream ended
  • Forget to stop tracks when leaving a page (memory & privacy)
  • Confuse active with track.enabled (mute vs end)
  • Call getUserMedia on plain HTTP (blocked in modern browsers)

Key Takeaways

Knowledge Unlocked

Five things to remember about active

Stream-level live status for media tracks.

5
Core concepts
⚙️02

Read-only

no setter

Access
⚠️03

Rule

any track live

Logic
📄04

Stop

track.stop()

Cleanup
🎯05

Baseline

since Sep 2017

Status

❓ Frequently Asked Questions

A read-only boolean. It is true when at least one MediaStreamTrack in the stream has a readyState other than "ended". When every track has ended, active becomes false.
No. MDN marks MediaStream.active as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
Call stop() on every track in the stream. When all tracks end, active flips to false and the inactive event fires.
No. Each track has its own readyState ("live" or "ended"). active is a stream-level summary: true if any track is still not ended.
Immediately after a successful getUserMedia() call, the returned MediaStream usually has live audio and/or video tracks, so active is typically true.
The active property itself works on any MediaStream object. Getting streams from getUserMedia() requires a secure context (HTTPS or localhost).
Did you know?

MDN’s active example disables the start button with startBtn.disabled = stream.active right after getUserMedia succeeds—a one-line UI guard for live camera streams.

Read the stream GUID

Learn MediaStream.id—the unique 36-character identifier on every stream.

id property →

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