MediaStreamTrack.stop() method stops the track.

句法

track.stop()
					

描述

调用 stop() tells the 用户代理 that the track's source—whatever that source may be, including files, network streams, or a local camera or microphone—is no longer needed by the MediaStreamTrack . Since multiple tracks may use the same source (for example, if two tabs are using the device's microphone), the source itself isn't necessarily immediately stopped. It is instead disassociated from the track and the track object is stopped. Once no media tracks are using the source, the source may actually be completely stopped.

Immediately after calling stop() readyState property is set to ended .

范例

Stopping a video stream

In this example, we see a function which stops a streamed video by calling stop() on every track on a given <video> .

function stopStreamedVideo(videoElem) {
  const stream = videoElem.srcObject;
  const tracks = stream.getTracks();
  tracks.forEach(function(track) {
    track.stop();
  });
  videoElem.srcObject = null;
}
					

This works by obtaining the video element's stream from its srcObject property. Then the stream's track list is obtained by calling its getTracks() method. From there, all that remains to do is to iterate over the track list using forEach() and calling each track's stop() 方法。

最后, srcObject 被设为 null to sever the link to the MediaStream object so it can be released.

规范

规范 状态 注释
媒体捕获和流
The definition of 'MediaStreamTrack.stop()' in that specification.
候选推荐 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
stop Chrome 61 Edge 12 Firefox 34 IE No Opera 45 Safari Yes WebView Android 61 Chrome Android 61 Firefox Android 34 Opera Android 43 Safari iOS Yes Samsung Internet Android 8.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: