aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/mediastream/gettracks/index.html
blob: c47d48473bfba253163d14857028afdf38126463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: MediaStream.getTracks()
slug: Web/API/MediaStream/getTracks
tags:
  - 参考
  - 媒体流
  - 媒体流 API
  - 媒体轨道
  - 方法
translation_of: Web/API/MediaStream/getTracks
---
<p>{{APIRef("Media Capture and Streams")}}{{SeeCompatTable}}</p>

<p>{{domxref("MediaStream")}} 接口的<strong><dfn><code>getTracks()</code></dfn></strong> 方法会返回一个包含  <code><a href="https://www.w3.org/TR/mediacapture-streams/#track-set">track set</a></code> 流中所有 {{domxref("MediaStreamTrack")}}  对象的序列, 序列内容与{{domxref("MediaStreamTrack.kind")}} 无关。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var <em>mediaStreamTracks</em> = <em>mediaStream</em>.getTracks()</pre>

<h3 id="参数">参数</h3>

<p></p>

<h3 id="返回值">返回值</h3>

<p>{{domxref("MediaStreamTrack")}} 对象的数组</p>

<h2 id="示例">示例</h2>

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

<h2 id="说明">说明</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">说明</th>
   <th scope="col">状态</th>
   <th scope="col">评论</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('Media Capture','#dom-mediastream-gettracks','getTracks()')}}</td>
   <td>{{Spec2('Media Capture')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持情况">浏览器支持情况</h2>

<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>

<p>{{Compat("api.MediaStream.getTracks")}}</p>