Class: AudioTrack

AudioTrack

An AudioTrack is a Track representing audio.


Properties:
Name Type Description
isStarted boolean

Whether or not the AudioTrack has started; if the AudioTrack started, there is enough audio data to begin playback

isEnabled boolean

Whether or not the AudioTrack is enabled; if the AudioTrack is not enabled, it is "muted"

kind Track.Kind

"audio"

mediaStreamTrack MediaStreamTrack

An audio MediaStreamTrack

Fires:

Extends

Methods


attach()

Create an HTMLAudioElement and attach the AudioTrack to it.

The HTMLAudioElement's srcObject will be set to a new MediaStream containing the AudioTrack's MediaStreamTrack.

Returns:

audioElement

Type
HTMLAudioElement
Example
const Video = require('twilio-video');

Video.createLocalAudioTrack().then(function(audioTrack) {
  const audioElement = audioTrack.attach();
  document.body.appendChild(audioElement);
});
  

attach(mediaElement)

Attach the AudioTrack to an existing HTMLMediaElement. The HTMLMediaElement could be an HTMLAudioElement or an HTMLVideoElement.

If the HTMLMediaElement's srcObject is not set to a MediaStream, this method sets it to a new MediaStream containing the AudioTrack's MediaStreamTrack; otherwise, it adds the MediaTrack'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
mediaElement HTMLMediaElement

The HTMLMediaElement to attach to

Returns:

mediaElement

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

const videoElement = document.createElement('video');
document.body.appendChild(videoElement);

Video.createLocalAudioTrack().then(function(audioTrack) {
  audioTrack.attach(videoElement);
});
  

attach(selector)

Attach the AudioTrack to an HTMLMediaElement selected by document.querySelector. The HTMLMediaElement could be an HTMLAudioElement or an HTMLVideoElement.

If the HTMLMediaElement's srcObject is not set to a MediaStream, this method sets it to a new MediaStream containing the AudioTrack's MediaStreamTrack; otherwise, it adds the AudioTrack'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:

mediaElement

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

const videoElement = document.createElement('video');
videoElement.id = 'my-video-element';
document.body.appendChild(videoElement);

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

detach()

Detach the AudioTrack from all previously attached HTMLMediaElements.

Returns:

mediaElements

Type
Array.<HTMLMediaElement>
Example
const mediaElements = audioTrack.detach();
mediaElements.forEach(mediaElement => mediaElement.remove());
  

detach(mediaElement)

Detach the AudioTrack from a previously attached HTMLMediaElement.

Parameters:
Name Type Description
mediaElement HTMLMediaElement

One of the HTMLMediaElements to which the AudioTrack is attached

Returns:

mediaElement

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

detach(selector)

Detach the AudioTrack from a previously attached HTMLMediaElement specified by document.querySelector.

Parameters:
Name Type Description
selector string

The query selector of HTMLMediaElement to which the AudioTrack is attached

Returns:

mediaElement

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

Events


disabled

The AudioTrack was disabled, i.e. "muted".

Parameters:
Name Type Description
track AudioTrack

The AudioTrack that was disabled


enabled

The AudioTrack was enabled, i.e. "unmuted".

Parameters:
Name Type Description
track AudioTrack

The AudioTrack that was enabled


started

The AudioTrack started. This means there is enough audio data to begin playback.

Parameters:
Name Type Description
track AudioTrack

The AudioTrack that started