Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch camera not working #170

Open
shohrejf opened this issue Jun 29, 2022 · 3 comments
Open

switch camera not working #170

shohrejf opened this issue Jun 29, 2022 · 3 comments

Comments

@shohrejf
Copy link

i am using this code in my project:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const [availableDeviceIds, setAvailableDeviceIds] = useState([]);
const [counter, setCounter] = useState(0);

useEffect(() => {
navigator.mediaDevices.enumerateDevices().then(mediaDevices => {
const videoDevices = mediaDevices.filter(x => x.kind === 'videoinput');
videoDevices.map((x: any, index) => {
setAvailableDeviceIds(x.deviceId);
});
});
}, [availableDeviceIds]);

const handleSwitchCamera = () => {
console.log('availableDeviceIds', availableDeviceIds);

if (onSwitchCamera) {
  onSwitchCamera();
}
if (availableDeviceIds?.lenght > 0) {
  if (counter === 0) {
    setCounter(1);
  } else if (counter === 1) {
    setCounter(0);
  }
} else {
  setCounter(0);
}

stream && stream.getTracks().forEach(stream => stream.stop());
if (availableDeviceIds?.lenght > 0) {
  turnOnCamera(availableDeviceIds[counter]);
} else {
  turnOnCamera(availableDeviceIds[0]);
}

};

const turnOnCamera = (deviceId: any) => {
if (onTurnOnCamera) {
onTurnOnCamera();
}
navigator.mediaDevices.enumerateDevices().then(mediaDevices => {
const fallbackContraints = {
audio: true,
video: true,
};
let currentConstraints: any;
if (availableDeviceIds?.lenght > 0) {
currentConstraints = merge(
{
video: {
deviceId: {
exact: availableDeviceIds[counter],
},
},
},
constraints,
);
} else {
currentConstraints = merge(
{
video: {
deviceId: {
exact: availableDeviceIds[0],
},
},
},
constraints,
);
}

  navigator.mediaDevices
    .getUserMedia(currentConstraints)
    .catch(err => {
      if (
        err.name === 'NotReadableError' ||
        err.name === 'OverconstrainedError'
      ) {
        console.warn(
          `Got ${err.name}, trying getUserMedia again with fallback constraints`,
        );
        return navigator.mediaDevices.getUserMedia(fallbackContraints);
      }
      throw err;
    })
    .then(handleSuccess);
});

};

const handleSuccess = stream => {
setStream(stream);
if (onCameraOn) {
onCameraOn();
}
// setSrc(stream);
// onTurnOnCamera();
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
and my videoRecorder is:

<VideoRecorder
constraints={{
audio: true,
video: true,
}}
src={src}
stream={stream}
/>

And after clicking on the button that calls the handleSwitchCamera function, the screen turns black.

@shohrejf shohrejf changed the title switch camera does not work in the video recorder switch camera not working Jun 29, 2022
@chiajunt
Copy link

Facing the same issue too, the camera switch does not seem to be working.

@dcorey05
Copy link

I'm seeing same issue, reverted to 3.18.4 and it works

@Laucsen
Copy link

Laucsen commented Dec 18, 2023

3.18.4 is working but not turn on the flash light on iPhone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@Laucsen @chiajunt @dcorey05 @shohrejf and others