MediaStreamTrack.stop() - Web APIs 编辑
The MediaStreamTrack.stop()
method stops the track.
Syntax
track.stop()
Description
Calling stop()
tells the user agent 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()
, the readyState
property is set to ended
.
Examples
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()
method.
Finally, srcObject
is set to null
to sever the link to the MediaStream
object so it can be released.
Specifications
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'MediaStreamTrack.stop()' in that specification. | Candidate Recommendation | Initial specification. |
Browser compatibility
BCD tables only load in the browser
See also
MediaStreamTrack
, the interface it belongs to.MediaStreamTrack.readyState
ended
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论