Warm tip: This article is reproduced from serverfault.com, please click

Webcam not working with JavaScript on some machines

发布于 2020-12-13 22:44:45

I'm building a an Electron application with Vue.js that uses a webcam. The webcam works within the Electron application on one computer but it just shows a black screen on another. The only notable difference (I think) is that the machine where it works (Machine A) uses Node v14.15.0 and on the machine that it doesn't work (Machine B) uses v12.18.4

I have tested the webcam on Machine B separately. It works via the native camera app on windows and on this online tool. The interesting thing is that both the integrated and external webcams fail to work. As soon as I start the stream the light comes on but that's it. It seems that the promise from .getUserMedia() isn't resolving (see code snippet) but I can't identify why.

How can I get the webcam to stream?

  let mediaInputs = [];
  let devices = [];

  if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
    console.log("enumerateDevices() not supported.");
    return;
  }

  mediaInputs = await navigator.mediaDevices.enumerateDevices();
  devices = await mediaInputs.filter(
    (device) => device.kind === "videoinput"
  );

       //Stop any existing streams
  if (this.video.srcObject !== undefined) {
    const l = this.video.srcObject;
    l.getTracks().forEach((track) => track.stop());
  }

  const sourceInfo = this.videoSources.find((o) => o.label === this.source);
  const constraints = {
    video: {
      deviceId: sourceInfo.deviceId,
      width: 1280, 
      height: 720,
    },
    audio: false,
  };
  try {
    console.log('This line is logged');
    //This is where I start the stream.

    const stream = await navigator.mediaDevices.getUserMedia(constraints); 
    console.log('This line is never reached');
    this.video = this.$refs.video; 
    this.video.srcObject = stream;
    this.video.play();
  } catch (error) {
    this.showSnackbar(error);
    console.error(error);
  }
Questioner
S.Ramjit
Viewed
0
S.Ramjit 2021-01-04 09:06:47

I just had to update to the latest version of Electron (11.1.1 at the time of writing) for it to work.

However if you're still having trouble, there's a GitHub thread on the topic that's still active