WebRTC API interface RTCTrackEvent 表示 track event, which is sent when a new MediaStreamTrack is added to an RTCRtpReceiver which is part of the RTCPeerConnection . The target is the RTCPeerConnection object to which the track is being added.

This event is sent by the WebRTC layer to the web site or application, so you will not typically need to instantiate an RTCTrackEvent yourself.

构造函数

RTCTrackEvent()
创建并返回新 RTCTrackEvent object, initialized with properties taken from the specified RTCTrackEventInit dictionary. You will probably not need to create new track events yourself, since they're typically created by the WebRTC infrastructure and sent to the connection's ontrack event handler.

特性

由于 RTCTrackEvent 基于 事件 , its properties are also available.

receiver 只读
RTCRtpReceiver used by the track that's been added to the RTCPeerConnection .
只读 可选
An array of MediaStream objects, each representing one of the media streams to which the added track belongs. By default, the array is empty, indicating a streamless track.
track 只读
MediaStreamTrack which has been added to the connection.
transceiver 只读
RTCRtpTransceiver being used by the new track.

Track event types

There is only one type of track event.

track

track event is sent to the RTCPeerConnection when a new track has been added to the connection. By the time the track event is delivered to the RTCPeerConnection 's ontrack handler, the new media has completed its negotiation for a specific RTCRtpReceiver (which is specified by the event's receiver 特性)。

此外, MediaStreamTrack specified by the receiver's track is the same one specified by the event's track , and the track has been added to any associated remote MediaStream 对象。

You can add a track event listener to be notified when the new track is available so that you can, for example, attach its media to a <video> element, using either RTCPeerConnection.addEventListener() ontrack event handler property.

注意: It may be helpful to keep in mind that you receive the track event when a new inbound track has been added to your connection, and you call addTrack() to add a track to the far end of the connection, thereby triggering a track event on the remote peer.

范例

This simple example creates an event listener for the track event which sets the srcObject <video> element with the ID videobox to the first stream in the list passed in the event's 数组。

peerConnection.addEventListener("track", e => {
  let videoElement = document.getElementById("videobox");
  videoElement.srcObject = e.streams[0];
}, false);
					

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCTrackEvent' 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
RTCTrackEvent Chrome 56 Edge ≤18 Firefox 22 IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android 44 Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0
receiver Chrome 56 Edge ≤18 Firefox 22 IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android 44 Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0
Chrome 56 Edge ≤18 Firefox 22 IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android 44 Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0
track Chrome 56 Edge ≤18 Firefox 22 IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android 44 Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0
transceiver Chrome 69 Edge ≤18 Firefox 59 IE No Opera 43 Safari Yes WebView Android 69 Chrome Android 69 Firefox Android 59 Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

元数据

  • 最后修改: