The getTrackById() method of MediaStream returns a single MediaStreamTrack that matches an id string, or null if none is found. Learn the MDN commentary ducking pattern, saving track.id from getUserMedia, null handling, and comparison with getTracks()—with five examples and try-it labs.
01
Kind
Instance method
02
Parameter
id string
03
Returns
Track or null
04
Lookup
by track.id
05
Use case
MDN ducking
06
Status
Baseline widely
Fundamentals
Introduction
Every MediaStreamTrack has a unique read-only id string. When a stream holds multiple tracks—main audio, commentary, camera, screen share—you often need to target one specific track later. getTrackById(id) does exactly that: pass the id, get the matching track, or null if it is not in this stream.
MDN’s example ducks the primary audio to 50% volume and enables a commentary track—both located by ids you saved earlier. Track ids are randomly generated by the browser, so store them when you first receive the stream rather than hard-coding UUIDs.
💡
Beginner tip
Always check for null before calling methods on the result: const track = stream.getTrackById(id); if (track) track.enabled = true;
Concept
Understanding MediaStream.getTrackById()
An instance method that looks up one track in the stream by its MediaStreamTrack.id value.
Parameters — id: a string identifying the track.
Return value — matching MediaStreamTrack, or null if no track has that id.
Match rule — compares against MediaStreamTrack.id (strict string match).
Scope — only tracks currently in this stream; ids from other streams return null.
Look up two tracks by stored ids (MDN pattern with placeholder UUIDs).
JavaScript
// Save real ids when you get the stream — these are MDN placeholders.
const primaryAudioTrack = stream.getTrackById(
"69f8520f-d94e-43f0-8a7c-77b1774f3b8f",
);
const commentaryTrack = stream.getTrackById(
"b5410643-2549-491e-b0f7-f08a4ebe54b8",
);
if (primaryAudioTrack) {
primaryAudioTrack.applyConstraints({ volume: 0.5 });
}
if (commentaryTrack) {
commentaryTrack.enabled = true;
}
MediaStream.getTrackById() 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.getTrackById()
Returns a MediaStreamTrack by id string, or null if not found.
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.getTrackById
Not supported
MediaStream.getTrackById()Excellent
Bottom line: Call getTrackById(id) to find one track when you know its MediaStreamTrack.id.
Wrap Up
Conclusion
MediaStream.getTrackById(id) finds one track in a stream by its MediaStreamTrack.id string. It returns the matching track or null. Save track ids when you first get a stream—MDN’s commentary example ducks primary audio and enables a second track by id.
Forget secure-context requirements for getUserMedia
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getTrackById()
Find one track in a stream by its id string.
5
Core concepts
🔎01
Param
id string
API
🔗02
Returns
track|null
Result
📦03
Null
no match
Edge
📄04
MDN
ducking
Pattern
🎯05
Baseline
since Sep 2017
Status
❓ Frequently Asked Questions
A MediaStreamTrack object whose id property matches the string you pass. If no track in the stream has that id, it returns null.
No. MDN marks MediaStream.getTrackById() as Baseline Widely available (since September 2017). It is not Deprecated, Experimental, or Non-standard.
One string argument: the track id to look up. MDN documents it as getTrackById(id).
Each MediaStreamTrack has a read-only id string (MediaStreamTrack.id). The browser generates it when the track is created. Save it when you first get the stream if you need to look the track up later.
getTracks() returns every track in the stream as an array. getTrackById(id) returns one track that matches the id, or null if none match.
Duck the main audio track volume to 50% with applyConstraints, then enable a commentary track—both found by stored track IDs with getTrackById().
Did you know?
MDN’s getTrackById() example lowers main audio to 50% with applyConstraints({ volume: 0.5 }) while enabling a commentary track—both found by ids you saved earlier.