mosca90 mosca90 - 1 month ago 19
Android Question

Voice recognition (speech to text - STT plugin)

I'm looking for speech recognition in Ionic2 framework with cordova.

If it can be implemented, could you gently provide an example of code (.html and .ts)?

I found this, but was for Ionic: http://devgirl.org/2016/01/08/speaking-with-cordova/ and i can't adapt the code for Ionic2.

I really appreciate any help you can provide, and sorry for my little english.

Andrea.

Answer

Source: Speech to text link.

Add plugin to your project:

cordova plugin add https://github.com/macdonst/SpeechRecognitionPlugin

Add declaration at the beginning of your .ts file, after import:

declare var SpeechRecognition: any;

Then your function:

recognition: any;

constructor() {}

SpeechToText() {
    this.platform.ready().then(() => {
        this.recognition = new SpeechRecognition(); 
        this.recognition.lang = 'en-US';
        this.recognition.onnomatch = (event => {
            console.log('No match found.');
        });
        this.recognition.onerror = (event => {
            console.log('Error happens.');
        });
        this.recognition.onresult = (event => {
            if (event.results.length > 0) {
                console.log('Text: ', event.results[0][0].transcript);          
            }
        });     
        this.recognition.start();
    });
}

Done.

Comments