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.

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 特性。

范例

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

规范

规范 状态 注释
媒体捕获和流
The definition of 'MediaStream.onaddtrack' in that specification.
候选推荐 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onaddtrack Chrome 26 Edge 12 Firefox 50 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 50 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: