简体   繁体   中英

restart stream after closing it by forEach(function(track) {track.stop();}

myVideoStream.getTracks().forEach(function(track) {
  track.stop();
}

how to restart the stream tracks after closing all the tracks. or any alternate method for turning on and turning off camera in video streaming app(like turn on turn off video button in google meet)

The documentation at MDN says the following about stopping the tracks.

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.

So that means that you have to restart the stream by creating a new one, because the media has completely stopped running.

I had the same problem, I solved it by using track.enable = false to mute, and using track.enable = true to reactivate, only it is necessary to save the original track in an independent variable before muting and using the track of this variable independent to reactivate, remembering that when reactivating it is necessary to use track.enable = true .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM