The MediaStream.onaddtrack
property is an event handler which specifies a function to be called when the addtrack
event occurs on a MediaStream
instance. This happens when a new track of any kind is added to the media stream.
This event is fired when the browser adds a track to the stream (such as when a RTCPeerConnection
is renegotiated or a stream being captured using HTMLMediaElement.captureStream()
gets a new set of tracks because the media element being captured loaded a new source.
The addtrack
event does not get fired when JavaScript code explicitly adds tracks to the stream (by calling addTrack()
).
MediaStream.onaddtrack = eventHandler;
This should be set to a function which you provide that accepts as input a MediaStreamTrackEvent
object representing the addtrack
event which has occurred. The MediaStreamTrack
representing the track which was added is specified in the event's track
property.
This example adds a listener which, when a new track is added to the stream, appends a new item to a list of tracks; the new item shows the track's kind
("audio"
or "video"
) and label
.
stream.onaddtrack = function(event) { let trackList = document.getElementById("tracks"); let label = document.createElement("li"); label.innerHTML = event.track.kind + ": " + event.track.label; trackList.appendChild(label); };
Specification |
---|
Media Capture and Streams # dom-mediastream-onaddtrack |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
onaddtrack |
26 |
12 |
50 |
No |
No |
11 |
37 |
26 |
50 |
No |
11 |
1.5 |
addtrack
event and its type, MediaStreamTrackEvent
.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream/onaddtrack