The removeTrack()
method of the MediaStream
interface removes a MediaStreamTrack
from a stream.
The removeTrack()
method of the MediaStream
interface removes a MediaStreamTrack
from a stream.
js
removeTrack(track)
track
A MediaStreamTrack
that will be removed from the stream.
None (undefined
).
The following example demonstrates how to remove the audio and video tracks from a MediaStream
. fetchStreamFunction
is an event handler for fetchStreamButton
. When the button is clicked, audio and video are captured from the system's devices. removeTracksFunction
is the event handler for removeTracksButton
. When this button is clicked, the audio and video tracks are removed from the MediaStream
.
js
let initialStream = null; let newStream = null; let fetchStreamButton = document.getElementById("fetchStream"); let removeTracksButton = document.getElementById("removeTracks"); async function fetchStreamFunction() { initialStream = await navigator.mediaDevices.getUserMedia({ video: { width: 620, height: 310 }, audio: true, }); if (initialStream) { await attachToDOM(initialStream); } } async function attachToDOM(stream) { newStream = new MediaStream(stream.getTracks()); document.querySelector("video").srcObject = newStream; } async function removeTracksFunction() { let videoTrack = newStream.getVideoTracks()[0]; let audioTrack = newStream.getAudioTracks()[0]; newStream.removeTrack(videoTrack); newStream.removeTrack(audioTrack); // Stream will be empty console.log(newStream.getTracks()); } fetchStreamButton.addEventListener("click", fetchStreamFunction); removeTracksButton.addEventListener("click", removeTracksFunction);
Specification |
---|
Media Capture and Streams # dom-mediastream-removetrack |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
removeTrack |
26 | 12 | 44 | No | 15 | 11 | 37 | 26 | 44 | No | 11 | 1.5 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream/removeTrack