1

I'm developing a speech recognition android plugin for a Phonegap application. I am actually new to Phonegap. Below is the important sections from my Java code. From this code, I need to call the Javascript function test.

public class CoolPlugin extends CordovaPlugin {

public static final String TAG = "Cool Plugin";
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";
private List<String> previousInterim;

private List<String>adapterList = new ArrayList<String>();



private static final String INITIALIZE = "init";
private static final String PLAY = "play";
private static final String STOP = "stop";


private AudioManager mAudioManager;
private int mStreamVolume = 0;
private Context context;



public boolean execute(final String action, JSONArray args, final CallbackContext callbackContext) throws JSONException
{


   if(PLAY.equals(action))
    {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                play(callbackContext);

            }
            });
    }
    return true;
}



public void play(CallbackContext callbackContext)
{

    //Start listening
    speech.stopListening();
    speech.cancel();
    speech.destroy();
    createRecog();
    speech.startListening(recognizerIntent);
    //adapter.clear();
}





private class RecognitionListenerClass implements RecognitionListener
{
    CallbackContext callbackContext;

    void onStartOfSpeechTimeout(CallbackContext callbackContext)
    {
        this.callbackContext = callbackContext;
    };


    @Override
    public void onPartialResults(Bundle arg0) {
        Log.i(LOG_TAG, "onPartialResults");
        final ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        final float[] scores = arg0.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
        receiveWhatWasHeard(matches, scores);


    }

}



private void receiveWhatWasHeard(ArrayList<String> matches, float[] scores) 
{

    //CALL THE JAVASCRIPT FUNCTION!!
}


}

From the method receiveWhatWasHeard, I need to call a javascript function. Since this is a speech application, please note that this call be will be called everytime it hear something.

Below is a part of my JavaScript code..

    var exec = require('cordova/exec');

function CoolPlugin() { 
 console.log("CoolPlugin.js: is created");
}



CoolPlugin.prototype.startRecognition = function(){
     console.log("CoolPlugin.js: showToast");

     exec(function(result){
         /*alert("OK" + reply);*/
       },
      function(result){
        /*alert("Error" + reply);*/
       },"CoolPlugin","play",[]);
    }



CoolPlugin.prototype.test = function(){
 //Do something and return information
}


 var coolPlugin = new CoolPlugin();
 module.exports = coolPlugin;

No need to say that most of the phonegap tutorials are totally outdated and I am struggling for this for the past few days. Any idea about how to implement this Java to Javascript callback?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • Though I'm not familiar with, what's that `CallbackContext` for? Use it to invoke JS callbacks. – frogatto Dec 30 '15 at 10:59
  • @HiI'mFrogatto: OK, how? There are lot of tutorials, pretty outdated. and as I mentioned, this won't simply stop by just giving one callback, because it will be invoked everytime it hear someone speaking, and will get invoked in mid-speak – PeakGen Dec 30 '15 at 11:08
  • 1
    Found the answer right in here - http://stackoverflow.com/a/19198844/1379286 – PeakGen Dec 30 '15 at 11:46

0 Answers0