MediaStream.getTracks()

The getTracks() method of the MediaStream interface returns a sequence that represents all the MediaStreamTrack objects in this stream's track set, regardless of MediaStreamTrack.kind.

Syntax

getTracks()

Parameters

None.

Return value

An array of MediaStreamTrack objects.

Examples

navigator.mediaDevices.getUserMedia({audio: false, video: true})
.then((mediaStream) => {
  document.querySelector('video').srcObject = mediaStream;
  // Stop the stream after 5 seconds
  setTimeout(() => {
    const tracks = mediaStream.getTracks()
    tracks[0].stop()
  }, 5000)
})

Specifications

Specification
Media Capture and Streams
# dom-mediastream-gettracks

Browser compatibility

BCD tables only load in the browser