Constructor
TrackEvent()-
Creates and initializes a new
TrackEventobject with the event type specified, as well as optional additional properties.
Instance properties
TrackEvent is based on Event, so properties of Event are also available on TrackEvent objects.
trackRead only-
The DOM track object the event is in reference to. If not
null, this is always an object of one of the media track types:AudioTrack,VideoTrack, orTextTrack).
Instance methods
TrackEvent has no methods of its own; however, it is based on Event, so it provides the methods available on Event objects.
Example
This example sets up a function, handleTrackEvent(), which is called for any addtrack or removetrack event on the first <video> element found in the document.
js
const videoElem = document.querySelector("video");
videoElem.videoTracks.addEventListener("addtrack", handleTrackEvent);
videoElem.videoTracks.addEventListener("removetrack", handleTrackEvent);
videoElem.audioTracks.addEventListener("addtrack", handleTrackEvent);
videoElem.audioTracks.addEventListener("removetrack", handleTrackEvent);
videoElem.textTracks.addEventListener("addtrack", handleTrackEvent);
videoElem.textTracks.addEventListener("removetrack", handleTrackEvent);
function handleTrackEvent(event) {
let trackKind;
if (event.target instanceof VideoTrackList) {
trackKind = "video";
} else if (event.target instanceof AudioTrackList) {
trackKind = "audio";
} else if (event.target instanceof TextTrackList) {
trackKind = "text";
} else {
trackKind = "unknown";
}
switch (event.type) {
case "addtrack":
console.log(`Added a ${trackKind} track`);
break;
case "removetrack":
console.log(`Removed a ${trackKind} track`);
break;
}
}
The event handler uses the JavaScript instanceof operator to determine which type of track the event occurred on, then outputs to console a message indicating what kind of track it is and whether it's being added to or removed from the element.
Specifications
| Specification |
|---|
| HTML # the-trackevent-interface |