mosca90 mosca90 - 22 days ago 7x
TypeScript Question

Voice recognition (speech to text) - Ionic2

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.



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