javascript 如何从 MediaStream 中删除曲目并“停止”网络摄像头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18174194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:57:58  来源:igfitidea点击:

How to remove track from MediaStream and "stop" webcam?

javascriptwebrtcgetusermedia

提问by Jonathan Azulay

I'm trying to remove a track from a MediaStream. MediaStream.removeTrack()removes the track from the stream, but the camera light is left on indicating that the camera is still active.

我正在尝试从 MediaStream 中删除曲目。MediaStream.removeTrack()从流中删除轨道,但相机灯仍然亮着,表明相机仍然处于活动状态。

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack?redirectlocale=en-US&redirectslug=DOM%2FMediaStreamTrack

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack?redirectlocale=en-US&redirectslug=DOM%2FMediaStreamTrack

This references a stop()method which I suppose would stop the camera completely, In chrome however I get "Object MediaStreamTrack has no method 'stop'"

这引用了stop()一种我认为可以完全停止相机的方法,但是在 chrome 中我得到了"Object MediaStreamTrack has no method 'stop'"

Is there a way around this or do I have to stop the whole stream and then recreate it with the tracks I don't want gone? As an example, I want to remove the video track while the audiotrack is still there.

有没有办法解决这个问题,还是我必须停止整个流,然后用我不想消失的轨道重新创建它?例如,我想在音轨仍然存在时删除视频轨道。

回答by Anand S

MediaStreamTrack.stop()is now addedto the Chrome.

MediaStreamTrack.stop()现在已添加到 Chrome 中。

MediaStream.stop()is deprecatedin Chrome 45.

MediaStream.stop()弃用在Chrome 45。

You should use MediaStream.getVideoTracks()to get video tracks and stop the track using MediaStreamTrack.stop()

您应该使用MediaStream.getVideoTracks()来获取视频轨道并使用MediaStreamTrack.stop()

回答by Sam Dutton

You need to call stop() on the MediaStream, not a MediaStreamTrack.

您需要在 MediaStream 上调用 stop(),而不是 MediaStreamTrack。

Take a look at simpl.info/gum. From the console, call stream.stop(): recording stops and the video camera light goes off.

看看simpl.info/gum。从控制台调用stream.stop():记录停止并且摄像机灯熄灭。

回答by jamix

It looks like the proper way to deal with this issue is to stop your MediaStream, recreate (and reattach) it as an audio-only one and then renegotiate the PeerConnectionsession. Unfortunately, Firefox currently doesn't supportrenegotiation mid-session.

看起来处理这个问题的正确方法是停止您的MediaStream,重新创建(并重新附加)它作为纯音频的,然后重新协商PeerConnection会话。不幸的是,Firefox 目前不支持在会话中重新协商。

The only viable hack is thus to also recreate the PeerConnectionwith the new MediaStreamas suggested here(see "Adding video mid-call").

因此,唯一可行的 hack 也是按照此处的建议重新创建PeerConnection新的(请参阅“添加视频通话中”)。MediaStream