library icon indicating copy to clipboard operation
library copied to clipboard

Starting camera after resetting throws an error when using Firefox mobile

Open Evaldash opened this issue 2 years ago • 2 comments

Describe the bug

Starting the scanner, after resetting it and using another camera, causes Firefox mobile to throw an error, that the video could not be started. No other browser I've tested throws such an error. Adding a 2 second delay after reset seems to make it work every time.

To Reproduce Steps to reproduce the behavior:

  1. Start the camera stream.
  2. Reset the qr scanner
  3. Start it again in the same function, programatically.

Expected behavior Camera feed appears in the video element, just like in Chrome.

Smartphone (please complete the following information):

  • Device: Samsung Galaxy S7, Samsung Galaxy Note 9
  • OS: Android 8, 10
  • Browser: Firefox mobile
  • Version: newest at the time of writing

Additional context Adding a 2 second delay before restarting the qr scanner seems to make it work.

Evaldash avatar Mar 16 '22 18:03 Evaldash

Here's a snippet, basically I keep the available cameras in a variable, and switch between them with a single button:

$("#next-cam-button").on("click", function () {
    if (canChangeCam){
        canChangeCam = false;
        codeReader.reset();

        selectedDeviceId = getNextCamera();

        showLoader();
  
        if (isFirefox && isAndroid){ // firefox bugs out otherwise?
          delay(2000).then( () => startScan());
        }
        else{ startScan(); }
    }
})

function getNextCamera(){
  currentCamIndex++;
  if (currentCamIndex >= availableCameras.length){
    currentCamIndex = 0;
  }
  return availableCameras[currentCamIndex].deviceId;
}

function startScan(){
    codeReader.decodeFromVideoDevice(selectedDeviceId, 'camera-stream', (result, err) => {
      if (result) {
        console.log(result)
        lastScannedString = result.text;
      }
      if (err && !(err instanceof ZXing.NotFoundException)) {
        console.error("Error: " +err);
      }
    })
    console.log(`Started continous decode from camera with id ${selectedDeviceId}`)
}

Evaldash avatar Mar 16 '22 19:03 Evaldash

Stale issue message

github-actions[bot] avatar Apr 29 '24 10:04 github-actions[bot]