I am trying to do some speech to text recognition using react native. I wrote a react module to start a recognizer intent
public class SpeechToTextModule extends ReactContextBaseJavaModule {
...
@ReactMethod
public void startListening(Callback errorCallback, Callback successCallback) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
if(getCurrentActivity() != null){
getReactApplicationContext().startActivityForResult(intent, SPEECH_REQUEST_CODE, null);
} else {
errorCallback.invoke(new NullPointerException("Activity is null"));
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
// control must come here
}
The mainactivity looks like this :
public class MainActivity extends ReactActivity {
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// how do i invoke the module function?
}
}
MainActivity receives the result from the voice intent, but I cant figure out how I can pass it over to the module.
There's a way to register as Activity event listener.
Add this to your Native Module:
public class SpeechToTextModule
extends ReactContextBaseJavaModule
implements ActivityEventListener {
public SpeechToTextModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this); //Register this native module as Activity result listener
}
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// Here is your Activity result :)
}
@Override
public void onNewIntent(Intent intent) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With