edimuj / cordova-plugin-audioinput

This iOS/Android Cordova/PhoneGap plugin enables audio capture from the device microphone, by in near real-time forwarding audio to the web layer of your application. A typical usage scenario for this plugin would be to use the captured audio as source for a web audio node chain, where it then can be analyzed, manipulated and/or played.
https://github.com/edimuj/app-audioinput-demo
MIT License
161 stars 88 forks source link

Ionic 4 - Browser audio input doesn't work #95

Open Merwan1010 opened 5 years ago

Merwan1010 commented 5 years ago

Hi,

I have a problem really similar to this post : https://github.com/edimuj/cordova-plugin-audioinput/issues/85

The plugin is working well on my android device but when i doionic cordova run browser and load the tab, i have this error in the console :

Screen Shot 2019-08-17 at 17 47 44

Here is the code in the ts file of the tab4 component :

declare let audioinput: any;

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

import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { File } from '@ionic-native/file/ngx';

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

  BUFFER_SIZE: number = 16384;

  constructor(private platform: Platform, private splashScreen: SplashScreen, private statusBar: StatusBar, private file: File) {
    platform.ready().then((source) => {
      console.log("platform source " + source);
    });
  }

  ngOnInit() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      window.addEventListener('audioinput', (event: any) => {
        //alert(JSON.stringify(event.data));
        alert( "Audio data received: " + event.data.length + " samples" );
      }, false);

      audioinput.checkMicrophonePermission((hasPermission) => {
        if (hasPermission) {
          console.log("We already have permission to record.");
          this.startCapture();
        }
        else {
          // Ask the user for permission to access the microphone
          audioinput.getMicrophonePermission((hasPermission, message) => {
            if (hasPermission) {
              console.log("User granted us permission to record.");
              this.startCapture();
            } else {
              console.warn("User denied permission to record.");
            }
          });
        }
      });
    });
  }

  public startCapture() {
    audioinput.start({
      bufferSize: this.BUFFER_SIZE,
      streamToWebAudio: false,
      normalize: true,
      channels: audioinput.CHANNELS.MONO,
      sampleRate: audioinput.SAMPLERATE.CD_AUDIO_44100Hz,
    });
  }

}

Do you know what cause the error ?

Thank you

edimuj commented 5 years ago

It doesn't seem to work either on an emulator or the browser. The point of the plugin is mainly to enable audio capture on iOS devices and older Android devices that haven't support for getUserMedia as most modern desktop browsers do.

Merwan1010 commented 5 years ago

Actually its working fine on ios emulator for me, on android emulator i have a mute microphone but maybe this is just some settings for the emulator. Thanks for your answer, i will use a different approach for browser.

numerized commented 4 years ago

Would be so awesome to make it work on the browser. It'll be so cool to have one codebase for all really.

numerized commented 4 years ago

@edimuj maybe you'd accept a little donation to make it work?

edimuj commented 4 years ago

@numerized well time is more of the issue, but donations are of course always happily accepted 🎁😊

Anyway, I will try to look into this in the coming weeks. Christmas is always a good time to do some housekeeping.

numerized commented 4 years ago

Cool! :)

nth-chile commented 4 years ago

It tries to do string replacement on fileURL, which is a problem if fileURL is null


// AudioInputCaptureProxy.js

96   fileUrl = opts[5] || fileUrl;
97   // the URL must be converted to a cdvfile:... URL to ensure it's readable from the outside
98   fileUrl = fileUrl.replace("filesystem:file:///", "cdvfile://localhost/");