Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. MediaStreamTrack
  4. stop()

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.

Syntax

js
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>.

js
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

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp