ionic-frameworkionic-nativecapacitor

Ionic v5 web media capture with capacitor - cordova_not_available


I am building an app with Ionic 5, Capacitor 2 and Angular 11. I need to capture video and audio using the media capture cordova plugin.

I installed the following modules:

npm install cordova-plugin-media-capture
npm install @ionic-native/media-capture

And added MediaCapture to the providers of my app module. Then I call mediaCapture.captureVideo() to retrieve a video ; unfortunately an exception is thrown when testing on a browser: cordova_not_available

The github repo states this plugin is web-compatible, and its sources have a browser implementation. However the window.navigator.device.capture is missing to make this plugin work.

Is it a bad configuration from my side? Or this cordova plugin wouldn't be compatible with capacitor?

I made a repro : https://stackblitz.com/edit/ionic-v5-media-capture-capacitor?file=src/app/app.component.ts

Thank you for your help


Solution

  • I wrote a regular Angular version for the web, if anyone need it:

    async browserCapture() {
      const stream: MediaStream = await navigator.mediaDevices.getUserMedia(this.constraints);
      const recorder = new MediaRecorder(stream, {mimeType: 'ogg'});
      const chunks: Blob[] = [];
      recorder.ondataavailable = (event: BlobEvent) => {
        if (event.data && event.data.size > 0) {
          chunks.push(event.data);
          this.zone.run(() => {
            this.recordChunks = [...chunks];
            this.cd.markForCheck();
          });
        }
      };
      this.recorder.onstop = () => {
        this.zone.run(() => {
          stream.getTracks().forEach(t => t.stop());
          if (chunks?.length) {
            this.upload(new Blob(chunks, {type: 'ogg'})); // use the blob result
          }
        });
      };
      recorder.start(500);
    }