简体   繁体   English

通过 forEach(function(track) {track.stop();} 关闭 stream 后重启

[英]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.如何在关闭所有轨道后重新启动 stream 轨道。 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)或在视频流应用程序中打开和关闭相机的任何替代方法(如在 google meet 中打开关闭视频按钮)

The documentation at MDN says the following about stopping the tracks. MDN文档说明了以下关于停止轨道的内容。

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.调用 stop() 告诉用户代理,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 .我有同样的问题,我通过使用track.enable = false静音,并使用track.enable = true重新激活解决了它,只需在静音和使用此轨道之前将原始轨道保存在自变量中变量独立于重新激活,记住重新激活时必须使用track.enable = true

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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