question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when asking for permission. DOMException: Could not start video source

See original GitHub issue

Hi,

I’m trying to use your component, however after updating to version 3.0.0 with Angular 9 and Ionic 5 being used in a PWA Site, I have the above mentioned error.

This error occurs only on the mobile (I’m using Android 8.0 with Chrome 84) but when I first invoke another page (of any other site) that uses the camera. So the error occurs. If however I invoke my page without any other page being invoked before, the component works.

In my page:

<zxing-scanner #scanner 
          start="true" 
          [autofocusEnabled]="true"
          [enable]="true"
          [device]="currentDevice"
          scannerEnabled="true"
          torch="on"
          [tryHarder]="true"
          (scanSuccess)="handleQrCodeResult($event)"
          [formats]="['QR_CODE', 'EAN_13', 'CODE_128', 'DATA_MATRIX']">
        </zxing-scanner>

Code behind:

import { Component, OnInit, ViewChild } from '@angular/core';

import { ZXingScannerComponent } from '@zxing/ngx-scanner';
import { Result } from '@zxing/library';
import { ModalController } from '@ionic/angular';

@Component({
  selector: 'app-zxing-test',
  templateUrl: './zxing-test.page.html',
  styleUrls: ['./zxing-test.page.scss'],
})
export class ZxingTestPage implements OnInit {

  @ViewChild('scanner', {static: true}) scanner: ZXingScannerComponent;

  hasDevices: boolean;
  hasPermission: boolean;
  qrResult: Result;
  availableDevices: MediaDeviceInfo[];
  currentDevice: MediaDeviceInfo = null;

  constructor(
    private modalCtrl: ModalController
  ) { }

  ngOnInit(): void {

    this.scannerInit();
  }

  scannerInit() {

     this.scanner.camerasFound.subscribe((devices: MediaDeviceInfo[]) => {
       this.hasDevices = true;
       this.availableDevices = devices;
       this.currentDevice = null;

      // this.compareWith = this.compareWithFn;
       this.onDeviceSelectChange(devices[devices.length - 1].deviceId);
     });

     this.scanner.camerasNotFound.subscribe(() => this.hasDevices = false);
     this.scanner.scanComplete.subscribe((result: Result) => this.qrResult = result);
     this.scanner.permissionResponse.subscribe((perm: boolean) => this.hasPermission = perm);
   }

   displayCameras(cameras: MediaDeviceInfo[]) {
    this.availableDevices = cameras;
  }

  handleQrCodeResult(resultString: string) {

    this.modalCtrl.dismiss({ valor: resultString });
  }

  onDeviceSelectChange(selected: string) {

  for (const device of this.availableDevices) {
      if (device.deviceId === selected) {
        this.currentDevice = device;
      }
    }
  }

  closeModal() {
    this.modalCtrl.dismiss({ valor: '' });
  }
}

The error occurs on getAnyVideoDevice() method of zxing-ngx-scanner.js:

    askForPermission() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.hasNavigator) {
                console.error('@zxing/ngx-scanner', 'Can\'t ask permission, navigator is not present.');
                this.setPermission(null);
                return this.hasPermission;
            }
            if (!this.isMediaDevicesSuported) {
                console.error('@zxing/ngx-scanner', 'Can\'t get user media, this is not supported.');
                this.setPermission(null);
                return this.hasPermission;
            }
            let stream;
            let permission;
            try {
                // Will try to ask for permission
                stream = yield this.getAnyVideoDevice();
                permission = !!stream;
            }
            catch (err) {
                return this.handlePermissionException(err);
            }
            finally {
                this.terminateStream(stream);
            }
            this.setPermission(permission);
            // Returns the permission
            return permission;
        });
    }
    /**
     *
     */
    getAnyVideoDevice() {
        return navigator.mediaDevices.getUserMedia({ video: true });
    }

Devtools view:

image

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ANTONIOFCcommented, Oct 25, 2020

Ok. Thnak you for the explanation.

1reaction
odahcamcommented, Oct 23, 2020

This error occurs only on the mobile (I’m using Android 8.0 with Chrome 84) but when I first invoke another page (of any other site) that uses the camera. So the error occurs. If however I invoke my page without any other page being invoked before, the component works.

Chrome only allows one page at a time to access media device’s stream, this is not this libraries fault, but a browser security behavior without a specific catchable error. Some versions of Chrome have better device handling others are worse, there’s nothing much we can do here other than somehow pass this error to who is using the component. Unfortunately the scanner won’t work when another page is using the camera in some browsers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DOMException: Could not start video source with zxing and ...
Typically that error happens when you do not close the front camera using stream.getTracks().forEach(track => track.stop() before calling ...
Read more >
DOMException: Could not start video source on Android ...
With camera and microphone permissions allowed. Some devices simply didn't display video to server, even on trace tools showing connected. },
Read more >
Can't access video, DOMException: Could not start video source
When I open our jitsi server in Android Chrome I can't access video, despite having granted permissions already (Site settings show Chrome ...
Read more >
NotReadableError: could not start video source - Google Groups
I am trying to develop Video Recording from Front as well as Rear Camera using WebRTC. Front camera recording & saving video works...
Read more >
Navigator.getUserMedia() - Web APIs | MDN
If permission is denied, no compatible input devices exist, or any other error condition occurs, the error callback is executed with a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found