MediaStream: addtrack event
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
The addtrack
event is fired when a new MediaStreamTrack
object has been added to a MediaStream
.
This event is not cancelable and does not bubble.
Syntax
Use the event name in methods like addEventListener()
, or set an event handler property.
js
addEventListener("addtrack", (event) => {});
onaddtrack = (event) => {};
Event type
A MediaStreamTrackEvent
. Inherits from Event
.
Event properties
Also inherits properties from its parent interface, Event
.
MediaStreamTrackEvent.track
Read only-
A
MediaStreamTrack
object representing the track which was added to the stream.
Examples
Using addEventListener()
:
js
const stream = new MediaStream();
stream.addEventListener("addtrack", (event) => {
console.log(`New ${event.track.kind} track added`);
});
Using the onaddtrack
event handler property:
js
const stream = new MediaStream();
stream.onaddtrack = (event) => {
console.log(`New ${event.track.kind} track added`);
};
Specifications
Specification |
---|
Media Capture and Streams # event-mediastream-addtrack |
Browser compatibility
BCD tables only load in the browser
See also
- Related events:
removetrack
- This event on
AudioTrackList
targets:addtrack
- This event on
VideoTrackList
targets:addtrack
- Media Capture and Streams API
- WebRTC