将视频约束设置为webrtc中的false之后,网络摄像头灯不会关闭

发布于 2025-01-20 12:22:48 字数 482 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-01-27 12:22:48

每次调用 navigator.mediaDevices.getUserMedia() 时,都会创建一个 new MediaStream,其中 new MediaStreamTracks。关闭这些新的 MediaStreamTracks 不会关闭以前的 MediaStreamTracks。

因此,您需要跟踪之前的 MediaStream,并从中关闭 VideoTrack。
鉴于您当前的代码,您似乎确实将其设置为 myVideo.current.srcObject,如果是这样,您可以从那里访问它:

const muteVideoTrack = () => {
  myVideo.current.srcObject.getVideoTracks().forEach((track) => track.stop());
};

或者您也可以简单地存储此 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:

const muteVideoTrack = () => {
  myVideo.current.srcObject.getVideoTracks().forEach((track) => track.stop());
};

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 of getVideoTracks(), and optionally set the srcObject to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文