将视频约束设置为webrtc中的false之后,网络摄像头灯不会关闭
我正在使用webrtc和socket.io进行视频通话。
呼叫工作正常,但是当我运行关闭相机功能时,视频停止了,但是我的网络摄像头的光仍在打开。
这是关闭相机的功能:
const mute = () => {
setMuteCamera(true);
navigator.mediaDevices
.getUserMedia({ video: false, audio: true })
.then((stream) => {
setStream(stream);
myVideo.current.srcObject = stream;
if (stream != null) {
stream?.getTracks().forEach((track) => {
track.stop();
});
}
});
};
I am using WebRTC and socket.io for video calling.
The call is working fine but when I run the turn off camera function, the video stops but the light of my webcam remains on.
Here is the function for turning off the camera:
const mute = () => {
setMuteCamera(true);
navigator.mediaDevices
.getUserMedia({ video: false, audio: true })
.then((stream) => {
setStream(stream);
myVideo.current.srcObject = stream;
if (stream != null) {
stream?.getTracks().forEach((track) => {
track.stop();
});
}
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
navigator.mediaDevices.getUserMedia()
时,都会创建一个 new MediaStream,其中 new MediaStreamTracks。关闭这些新的 MediaStreamTracks 不会关闭以前的 MediaStreamTracks。因此,您需要跟踪之前的 MediaStream,并从中关闭 VideoTrack。
鉴于您当前的代码,您似乎确实将其设置为
myVideo.current.srcObject
,如果是这样,您可以从那里访问它:或者您也可以简单地存储此 MediaStream在您的方法可访问的任何变量中。
这将使MediaStream仅捕获麦克风。如果您想完全停止整个 MediaStream,请调用
getTracks()
而不是getVideoTracks()
,并可选择将srcObject
设置为空。
Every time you call
navigator.mediaDevices.getUserMedia()
a new MediaStream is created, with new MediaStreamTracks. Closing these new MediaStreamTracks won't close the previous ones.So what you need is to keep track of your previous MediaStream, and close the VideoTrack from it.
Given your current code it seems that you do set it as
myVideo.current.srcObject
, if so, you can access it from there:Or you could also simply store this MediaStream in any variable accessible to your method.
This will let the MediaStream capture only the microphone. If you wanted to completely stop the whole MediaStream, then call
getTracks()
instead ofgetVideoTracks()
, and optionally set thesrcObject
tonull
.