MediaStream.getTracks()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

MediaStream 接口的**getTracks()** 方法会返回一个包含 track set 流中所有 MediaStreamTrack 对象的序列,序列内容与MediaStreamTrack.kind 无关。

语法

js
getTracks()

参数

返回值

MediaStreamTrack 对象的数组

示例

js
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);
  });

规范

Specification
Media Capture and Streams
# dom-mediastream-gettracks

浏览器兼容性

BCD tables only load in the browser