pbakondy / cordova-plugin-speechrecognition

:microphone: Cordova Plugin for Speech Recognition
MIT License
197 stars 117 forks source link

How to identify who has stopped recording? #72

Open DeeSouza opened 6 years ago

DeeSouza commented 6 years ago

I'm using Ionic 3 and plugin native Speech Recognition: Plugin Ionic Native

My device is an Android 7.0.

So, how to identify who has stopped recording?

The function stopListening is only iOS. But, and in Android?

Thanks in advanced.

texano00 commented 6 years ago

Hi @DeeSouza, on Android the speech recognition (google assistant technology) auto-stop the recording when the user stop talking while on IOS (siri technology) you have to manually stop the recording through the stopListening method.

Maybe the answer of your question is you have to switch your code execution calling platform.is('ios') and platform.is('android') and use stopListening only on IOS.

DeeSouza commented 6 years ago

Hi @texano00,

Thank you for answer.

But, how would I do when use the parameter showPartial ?

Don't have my code now, but is like this:

isRecording = false;

function startListen()
{
      isRecording = true;

      optionsAndroid = {
         showPartial: true,
         showPopup: false
      };
     this.speech.startListening(optionsAndroid).subscribe(
      matches => {
           isRecording = false;
           this.speechResult = matches[0];
           this.cd.detectChanges();
      }
   );
}
texano00 commented 6 years ago

I don't know what your application do, but from the doc

If you set showPartial to true on iOS the success callback will be called multiple times until stopListening() called.

I suppose that showPartial has the same effect on Android but i'm not sure.

DeeSouza commented 6 years ago

In monday I post my code and explain better.

Thanks!

DeeSouza commented 6 years ago

My code:

// Listen to user
listenForSpeech() {
    this.androidOptions = {
        language: 'pt-BR',
        matches: 1,
        showPopup: false,
        **showPartial: true** // ---------------------------------- SHOW PARTIAL
    };

    this.iosOptions = {
        matches: 1,
        language: 'pt-BR',
        **showPartial: true** // ---------------------------------- SHOW PARTIAL
    };

    if(this.platform.is('android')){
        return this.speech.startListening(this.androidOptions);
    }
    else if(this.platform.is('ios')){
        return this.speech.startListening(this.iosOptions);
    }
}
// Listen to user
async manualSpeech(event){
    this.isRecording = true;

    this.speech.listenForSpeech().subscribe(
        async matches   => this.searchManual(matches),
        error           => {
            console.log(error);
            this.isRecording = false;
        }
    );
}

// Search query
async searchManual(matches){
    this.speechResult = matches[0];

    if(matches && matches.length > 0){
        this.zone.run(() => {
            this.isRecording    = false;
            console.log('Send Message'); // --------------------- HERE -------------------------
        });
    }
}

I'm using the parameter showPartial . If I to told "The books is on the table", the console.log('Send Message'); will appear 6 times.

But, I want what this happens only when to stop speech, in ANDROID, so so, only one time.