Class: Track

Track

A Track represents audio or video that can be sent to or received from a Room.


Properties:
Name Type Description
id Track.ID

This Track's ID

isStarted boolean

Whether or not the Track has started

isEnabled boolean

Whether or not the Track is enabled (i.e., whether it is paused or muted)

kind string

The kind of the underlying MediaStreamTrack; e.g. "audio" or "video"

mediaStreamTrack MediaStreamTrack

The underlying MediaStreamTrack

Fires:

Methods


attach(selector)

Attach the Track to an HTMLMediaElement selected by document.querySelector.

If the HTMLMediaElement's srcObject is not set to a MediaStream, this method sets it to a new MediaStream containing the Track's MediaStreamTrack; otherwise, it adds the Track's MediaStreamTrack to the existing MediaStream. Finally, if there are any other MediaStreamTracks of the same kind on the MediaStream, this method removes them.

Parameters:
Name Type Description
selector string

A query selector for the HTMLMediaElement to attach to

Returns:
Type
HTMLMediaElement
Example
var Video = require('twilio-video');

Video.createLocalAudioTrack().then(function(track) {
  track.attach('#my-existing-video-element-id');
});

attach()

Attach the Track to a newly created HTMLMediaElement.

The HTMLMediaElement's srcObject will be set to a new MediaStream containing the Track's MediaStreamTrack.

Returns:

Either an HTMLAudioElement or HTMLVideoElement, depending on the Track's kind

Type
HTMLMediaElement
Example
var Video = require('twilio-video');

Video.createLocalVideoTrack().then(function(track) {
  var videoElement = track.attach();
  document.getElementById('my-container').appendChild(videoElement);
});
 

attach(el)

Attach the Track to an existing HTMLMediaElement.

If the HTMLMediaElement's srcObject is not set to a MediaStream, this method sets it to a new MediaStream containing the Track's MediaStreamTrack; otherwise, it adds the Track's MediaStreamTrack to the existing MediaStream. Finally, if there are any other MediaStreamTracks of the same kind on the MediaStream, this method removes them.

Parameters:
Name Type Description
el HTMLMediaElement

The HTMLMediaElement to attach to

Returns:
Type
HTMLMediaElement
Example
var Video = require('twilio-video');
var videoElement;

Video.createLocalVideoTrack().then(function(track) {
  videoElement = track.attach();
  document.getElementById('my-container').appendChild(videoElement);
  return Video.createLocalAudioTrack();
}).then(function(track) {
  track.attach(videoElement);
});
 

detach()

Detach a Track from all previously attached HTMLMediaElements.

Returns:

The detachedHTMLMediaElements

Type
Array.<HTMLMediaElement>
Example
var detachedElements = track.detach();
detachedElements.forEach(function(el) {
  el.remove();
});
 

detach(el)

Detach a Track from a previously attached HTMLMediaElement.

Parameters:
Name Type Description
el HTMLMediaElement

One of the HTMLMediaElements to which the Track is attached

Returns:

The detached HTMLMediaElement

Type
HTMLMediaElement
Example
var videoElement = document.getElementById('my-video-element');
track.detach(videoElement).remove();
 

detach(selector)

Detach a Track from a previously attached HTMLMediaElement specified by document.querySelector.

Parameters:
Name Type Description
selector string

The query selector of HTMLMediaElement to which the Track is attached

Returns:

The detached HTMLMediaElement

Type
HTMLMediaElement
Example
track.detach('#my-video-element').remove();

Type Definitions


ID

The Track ID is a string identifier for the Track.

Type:
  • string

Events


disabled

The Track was disabled. For AudioTracks this means "muted", and for VideoTracks this means "paused".

Parameters:
Name Type Description
track Track

The Track that was disabled


enabled

The Track was enabled. For AudioTracks this means "unmuted", and for VideoTracks this means "unpaused".

Parameters:
Name Type Description
track Track

The Track that was enabled


started

The Track started. This means that the Track contains enough audio or video to begin playback.

Parameters:
Name Type Description
track Track

The Track that started