mosca90 mosca90 - 11 months ago 84
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: and i can't adapt the code for Ionic2.

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


Answer Source

Source: Speech to text link.

Add plugin to your project:

cordova plugin add

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);