MediaStream.onaddtrack - Web APIs 编辑

The MediaStream.onaddtrack property is an EventHandler 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()).

Syntax

MediaStream.onaddtrack = eventHandler;

Value

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.

Example

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);
};

Specifications

SpecificationStatusComment
Media Capture and Streams
The definition of 'MediaStream.onaddtrack' in that specification.
Candidate RecommendationInitial specification.

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:129 次

字数:4019

最后编辑:8年前

编辑次数:0 次

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