TrackEvent - Web APIs 编辑

The TrackEvent interface, which is part of the HTML DOM specification, is used for events which represent changes to a set of available tracks on an HTML media element; these events are addtrack and removetrack. It's important not to confuse TrackEvent with the RTCTrackEvent interface, which is used for tracks which are part of an RTCPeerConnection.

Events based on TrackEvent are always sent to one of the media track list types:

Constructor

TrackEvent()
Creates and initializes a new TrackEvent object with the event type specified, as well as optional additional properties.

Properties

TrackEvent is based on Event, so properties of Event are also available on TrackEvent objects.

track Read 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, or TextTrack).

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 callled for any addtrack or removetrack event on the first <video> element found in the document.

var videoElem = document.querySelector("video");

videoElem.videoTracks.addEventListener("addtrack", handleTrackEvent, false);
videoElem.videoTracks.addEventListener("removetrack", handleTrackEvent, false);
videoElem.audioTracks.addEventListener("addtrack", handleTrackEvent, false);
videoElem.audioTracks.addEventListener("removetrack", handleTrackEvent, false);
videoElem.textTracks.addEventListener("addtrack", handleTrackEvent, false);
videoElem.textTracks.addEventListener("removetrack", handleTrackEvent, false);

function handleTrackEvent(event) {
  var 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

SpecificationStatusComment
HTML Living Standard
The definition of 'TrackEvent' in that specification.
Living StandardInitial definition.
HTML5
The definition of 'TrackEvent' in that specification.
RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:105 次

字数:6368

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文