大网数据广告活动
Web 标准与前端开发

RTCTrackEvent

· 52CE 我爱测

The WebRTC API interface **`RTCTrackEvent`** represents the RTCPeerConnection.track_event event, which is sent when a new MediaStreamTrack is added to an RTCRtpReceiver which is part of the RTCPeerConnection.

WebRTC

The WebRTC API interface RTCTrackEvent represents the RTCPeerConnection.track_event 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 website or application, so you will not typically need to instantiate an RTCTrackEvent yourself.

Constructor

  • RTCTrackEvent.RTCTrackEvent
    • : Creates and returns a new RTCTrackEvent object. 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 RTCPeerConnection.track_event event handler.

Instance properties

Since RTCTrackEvent is based on Event, its properties are also available.

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

Track event types

There is only one type of track event.

track

The RTCPeerConnection.track_event 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 RTCPeerConnection.track_event handler, the new media has completed its negotiation for a specific RTCRtpReceiver (which is specified by the event's RTCTrackEvent.receiver property).

In addition, the MediaStreamTrack specified by the receiver's RTCRtpReceiver.track is the same one specified by the event's RTCTrackEvent.track, and the track has been added to any associated remote MediaStream objects.

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 EventTarget.addEventListener or the ontrack event handler property.

[!NOTE] 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 RTCPeerConnection.addTrack to add a track to the far end of the connection, thereby triggering a track event on the remote peer.

Example

This simple example creates an event listener for the RTCPeerConnection.track_event event which sets the HTMLMediaElement.srcObject of the video element with the ID video-box to the first stream in the list passed in the event's RTCTrackEvent.streams array.

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

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。