Speech Recognition (SOLVED)

Hello!

I has a problem with Speech Recognition in my ionic app.

I use webkitSpeechRecognition and it’s work fine using chrome in the computer with ionic server but, when I build the android app and I try it in my Nexus 5, I obtain the next error: ReferenceError: webkitSpeechRecognition is not defined

It is possible use webkitSpeechRecognition with ionic apps?

If not, there is another way to implement Speech Recognition in android and iOS ionic apps?

Thanks!
Javi.

1 Like

Hi,

You need to use cordova plugin for that. there are couple of them open sourced. you can do some search…

Here’s one i found…

Sorry!

I modified the code changing:
var recognition = new SpeechRecognition(); => var recognition = new webkitSpeechRecognition();

Then, it’s works in computer.

Using: var recognition = new SpeechRecognition(); It works in mobile.

Thanks a lot!

1 Like

can you share how to use this sir ? :smile:

@losewin,
First, install the plugin: cordova plugin add https://github.com/macdonst/SpeechRecognitionPlugin

Then, you can use in the controller:

$scope.recognizedText = "";
    
$scope.record = function() {
    //var recognition = new webkitSpeechRecognition(); //To Computer
    var recognition = new SpeechRecognition(); // To Device
    recognition.lang = 'es-ES';
    
    recognition.onresult = function(event) {
        if (event.results.length > 0) {
            $scope.recognizedText = event.results[0][0].transcript;
            $scope.$apply();
        }
    };
    
    recognition.start();
};

And index.hml

<input type="text" id="entrada" value="{{ recognizedText }}"><BR>
<button class="button" id="boton" ng-click="record()">Start</button>
2 Likes

Thanks alot for sharing :smile:

after I install the plugin in cordova, what I must do to use this plugin? where the controller is? I’m brand new in cordova, can you give me the tutorial from the start again? because I want to build app with speech recognition too, thanks for your help

have you ever try that plug in? I tried that but there is no result, can you help me?

You can start with this: https://ccoenraets.github.io/ionic-tutorial/

I too tried but dosen’t seem to work.I have posted in Stackoverflow…

what about with ionic 2?

1 Like

@arsene123 checkout this question http://stackoverflow.com/questions/38571063/voice-recognition-speech-to-text-stt-plugin

i try this but my app stop working

Another alternative if you want to implement Web Speech Recognition quickly and without headache in your ionic app, is to use ng-speech-recognition directive: https://github.com/Devniz/ng-speech-recognition

i’m working with ionic 2

In my app (Ionic 2 >= RC2, android) this works:

Plugin: org.apache.cordova.speech.speechrecognition

after import:
declare var SpeechRecognition: any;

speechToText() {
    this.platform.ready().then(() => {
      this.recognition = new SpeechRecognition();
      this.recognition.lang = 'it-IT';
      this.recognition.onnomatch = (event => {
        this.showAlert('No match found.');
      });
      this.recognition.onerror = (event => {
        this.showAlert('Error happens.');
      });
      this.recognition.onresult = (event => {
        if (event.results.length > 0) {
          this.loadWords(event.results[0][0].transcript);
        }
      });
      this.recognition.start();
    });
 }

loadWords(dati: string) {
    // bla bla bla
}

I hope it helps.

1 Like

let me time try it, great thanks

Hello! I cant install plugin: org.apache.cordova.speech.speechrecognition (Error: Registry returned 404 for GET on https://registry.npmjs.org/org.apache.cordova.speech.speechrecognition)
so… I have installed this one: cordova plugin add GitHub - macdonst/SpeechRecognitionPlugin: W3C Web Speech API - Speech Recognition plugin for PhoneGap
and like the example, my code is:

import { Component } from ‘@angular/core’;

import { NavController, Platform } from ‘ionic-angular’;

declare var SpeechRecognition: any;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {

constructor(public navCtrl: NavController, public platform: Platform) {

}

speechToText() {
this.platform.ready().then(() => {
this.recognition = new SpeechRecognition();
this.recognition.lang = ‘it-IT’;
this.recognition.onnomatch = (event => {
this.showAlert(‘No match found.’);
});
this.recognition.onerror = (event => {
this.showAlert(‘Error happens.’);
});
this.recognition.onresult = (event => {
if (event.results.length > 0) {
this.loadWords(event.results[0][0].transcript);
}
});
this.recognition.start();
});
}

loadWords(dati: string) {
// bla bla bla
}

}

But I have this message: "Property ‘recognition’ does not exist on type ‘HomePage’.
Somebody know how can I solve it?
Thanks in advance!

Jose, the command actually is cordova plugin add GitHub - macdonst/SpeechRecognitionPlugin: W3C Web Speech API - Speech Recognition plugin for PhoneGap and it install the org.apache.cordova.speech.speechrecognition as you can see in plugin directory of the project, didn’t you?

Add recognition as var:

you have :
export class HomePage {

constructor(public navCtrl: NavController, public platform: Platform) {

}

speechToText() {
this.platform.ready().then(() => {
this.recognition = new SpeechRecognition();
this.recognition.lang = 'it-IT';

insert:

export class HomePage {

recognition;

constructor(public navCtrl: NavController, public platform: Platform) { }

speechToText() {
    this.platform.ready().then(() => {
    this.recognition = new SpeechRecognition();

So you declare recognition as property. I’m sorry, many times we omit some basics…
BTW, I think you have to change this.recognition.lang= “it-IT” with “es-ES” or the exact language you is speaking and the country acronym (example it-CH for italian language in Switzerland).

I had some problem after upgrade from RC2 to RC4. If I compile with RC2 it run, but with RC4 I got, in console, the run time error:

EXCEPTION: Uncaught (in promise): ReferenceError: SpeechRecognition is not defined

I haven’t fixed it yet. I post the help request here.

P.

Thanks for reply me pimol, now my code is like this:

import { Component } from ‘@angular/core’;

import { NavController, Platform } from ‘ionic-angular’;

declare var SpeechRecognition: any;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {

recognition: any;

constructor(public navCtrl: NavController, public platform: Platform) {

}

speechToText() {
this.platform.ready().then(() => {
this.recognition = new SpeechRecognition();
this.recognition.lang = ‘it-IT’;
this.recognition.onnomatch = (event => {
//this.showAlert(‘No match found.’);
});
this.recognition.onerror = (event => {
//this.showAlert(‘Error happens.’);
});
this.recognition.onresult = (event => {
if (event.results.length > 0) {
this.loadWords(event.results[0][0].transcript);
}
});
this.recognition.start();
});
}

loadWords(dati: string) {
// bla bla bla
}

}

and I have this line in my android.json after execute the new command:

    "org.apache.cordova.speech.speechrecognition": {
        "PACKAGE_NAME": "com.ionicframework.speed802926"
    }

Problem now is: “EXCEPTION: Uncaught (in promise): ReferenceError: SpeechRecognition is not defined”
You know why?