mosca90
mosca90

Reputation: 999

Voice recognition (speech to text - STT cordova plugin)

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

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

I found this, but was for Ionic1: 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.

Upvotes: 0

Views: 5904

Answers (2)

mosca90
mosca90

Reputation: 999

Source: https://github.com/macdonst/SpeechRecognitionPlugin.

Using command line, add this plugin to your Ionic2 project:

cd Your_Project_Root_Folder

Since iOS 10 it's mandatory to add a NSMicrophoneUsageDescription in the info.plist to access the microphone.

To add this entry you can pass the MICROPHONE_USAGE_DESCRIPTION variable on plugin install.

ionic plugin add https://github.com/macdonst/SpeechRecognitionPlugin --variable MICROPHONE_USAGE_DESCRIPTION="your usage message"

On iOS 10 and greater it uses the native SFSpeechRecognizer (same as Siri). On iOS 9 and older it uses iSpeech SDK, an API key is required, get one on https://www.ispeech.org/, it's free. To provide the key, add this preference inside the config.xml

<preference name="apiKey" value="yourApiKeyHere" />

Add declaration at the beginning of your .ts file, just after import, before class definition:

declare const SpeechRecognition: any;

Then, in your class:

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('Output STT: ', event.results[0][0].transcript);            
            }
        });     
        this.recognition.start();
    });
}

iSpeech supported languages are: English (Canada) (en-CA) English (United States) (en-US) Spanish (Spain) (es-ES) French (France) (fr-FR) Italian (Italy) (it-IT) Polish (Poland) (pl-PL) Portuguese (Portugal) (pt-PT)

ps: For iOS 10 error kAFAssistantErrorDomain or if you have to wait for results, check this.

Done!

edit: tested on Ionic v3.0.1 (2017-04-06) and works fine :)

Upvotes: 5

Nizar B.
Nizar B.

Reputation: 3118

I use this AngularJS directive:

ng-speech-recognition

Upvotes: 1

Related Questions