MediaStreamTrack: stop() method
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.
Thestop() method of theMediaStreamTrack interface stops the track.
In this article
Syntax
stop()Parameters
None.
Return value
None (undefined).
Description
Callingstop() tells theuser agent that the track'ssource—whatever that source may be, including files, network streams, or a local cameraor microphone—is no longer needed by theMediaStreamTrack. Since multipletracks may use the same source (for example, if two tabs are using the device'smicrophone), the source itself isn't necessarily immediately stopped. It is insteaddisassociated from the track and the track object is stopped. Once no media tracks areusing the source, the source may actually be completely stopped.
Immediately after callingstop(), thereadyState property is set toended. Note that theended event will not be fired in this situation.
Examples
>Stopping a video stream
In this example, we see a function which stops a streamed video by callingstop() on every track on a given<video>.
function stopStreamedVideo(videoElem) { const stream = videoElem.srcObject; const tracks = stream.getTracks(); tracks.forEach((track) => { track.stop(); }); videoElem.srcObject = null;}This works by obtaining the video element's stream from itssrcObject property. Then the stream's tracklist is obtained by calling itsgetTracks()method. From there, all that remains to do is to iterate over the track list usingforEach() and calling each track'sstop()method.
Finally,srcObject is set tonull to sever the link to theMediaStream object so it can be released.
Specifications
| Specification |
|---|
| Media Capture and Streams> # dom-mediastreamtrack-stop> |
Browser compatibility
See also
MediaStreamTrack, the interface it belongs to.MediaStreamTrack.readyStateended