Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

RTCPeerConnection: track event

BaselineWidely available

Thetrack event is sent to theontrack event handler onRTCPeerConnections after a new track has been added to anRTCRtpReceiver which is part of the connection.

By the time this event is delivered, the new track has been fully added to the peer connection. SeeTrack event types for details.

This event is not cancellable and does not bubble.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("track", (event) => { })ontrack = (event) => { }

Event type

Event properties

SinceRTCTrackEvent is based onEvent, its properties are also available.

receiverRead only

TheRTCRtpReceiver used by the track that's been added to theRTCPeerConnection.

streamsRead onlyOptional

An array ofMediaStream objects, each representing one of the media streams to which the addedtrack belongs. By default, the array is empty, indicating a streamless track.

trackRead only

TheMediaStreamTrack which has been added to the connection.

transceiverRead only

TheRTCRtpTransceiver being used by the new track.

Examples

This example shows code that creates a newRTCPeerConnection, then adds a newtrack event handler.

js
pc = new RTCPeerConnection({  iceServers: [    {      urls: "turn:fake.turn-server.url",      username: "some username",      credential: "some-password",    },  ],});pc.addEventListener(  "track",  (e) => {    videoElement.srcObject = e.streams[0];    hangupButton.disabled = false;  },  false,);

The event handler assigns the new track's first stream to an existing<video> element, identified using the variablevideoElement.

You can also assign the event handler function to theontrack property, rather than useaddEventListener().

js
pc.ontrack = (e) => {  videoElement.srcObject = e.streams[0];  hangupButton.disabled = false;  return false;};

Specifications

Specification
WebRTC: Real-Time Communication in Browsers
# event-track

Browser compatibility

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp