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
Fundamentals
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())
Concept
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.
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNo MediaStream.getTracks
Not supported
MediaStream.getTracks()Excellent
Bottom line: Call getTracks() to list, inspect, or stop every track in a MediaStream.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getTracks()
List and control every track in a stream.
5
Core concepts
📋01
Returns
array
API
🔗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.