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
How to remove track from MediaStream and "stop" webcam?
提问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()
从流中删除轨道,但相机灯仍然亮着,表明相机仍然处于活动状态。
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 PeerConnection
session. Unfortunately, Firefox currently doesn't supportrenegotiation mid-session.
看起来处理这个问题的正确方法是停止您的MediaStream
,重新创建(并重新附加)它作为纯音频的,然后重新协商PeerConnection
会话。不幸的是,Firefox 目前不支持在会话中重新协商。
The only viable hack is thus to also recreate the PeerConnection
with the new MediaStream
as suggested here(see "Adding video mid-call").
因此,唯一可行的 hack 也是按照此处的建议重新创建PeerConnection
新的(请参阅“添加视频通话中”)。MediaStream