How to implement custom Speech Recognition Intent


#1

Hi guys,

I’m trying to implement speech recognition and I have problems in trying to implement it.

I found an implementation that seemingly fits my needs https://github.com/hiteshsahu/Android-TTS-STT/blob/master/app/src/main/java/com/tcs/speechtotextpoc/translation_engine/translators/SpeechToTextConvertor.java
but I don’t know how to implement it Nativescript.

I have a few questions regarding the implementation of the code and doing it in Typescript:

How do I pass in the app context?
I tried passing in app.android.currentContext, but it only crashes the app.
How do I implement this line of code?
class CustomRecognitionListener implements RecognitionListener

Those are the ones I can’t figure out for now although I am still clueless about Native APIs and would highly appreciate your help in implementing a custom Speech Recognition Intent.

Thank you


#2

… but it only crashes the app.

Is not enough information to work on. How does it crash? Is there a particular exception message? Are there any logs?

See if the article at http://docs.nativescript.org/runtimes/android/generator/extend-class-interface#interfaces clears up some of the questions you might have.

How do I implement this line of code?

You need to dive a little into the Android reference to see what classes are exposed, what their namespaces are (the full lengthy dot-separated names).

A simple Google search for RecognitionListener yielded this -> https://developer.android.com/reference/android/speech/RecognitionListener.html

Now it becomes clear that the RecognitionListener is an interface, and I see all its methods that I would have to implement.

Depending on whether you want to implement the interface directly or extend a class, and then implement the interface you could do the following in JavaScript:

Implement:

var myCustomRecognitionListener = new android.speech.RecognitionListener({
   onBeginningOfSpeech: function() { },
   onEndOfSpeech: function() { },
   ...
});

or Extend:

var CustomRecognitionListener = java.lang.Object.extend("my.custom.namespace.CustomRecognitionListener", {
   interfaces: [android.speech.RecognitionListener],
   onBeginningOfSpeech: function() { },
   onEndOfSpeech: function() { },
   ...
   // any other custom implementation  you'd like to bring along from the extended class
   // in this simple example - the Object class
});

Note that it is important to specify the full namespaces to classes/interfaces when calling to Android/Java api, you can normally save those in constant variable for easier access, like

const RecognitionListener = android.speech.RecognitionListener