diff --git a/plugin.xml b/plugin.xml index 6fae46b..ae92c8f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="0.1.2"> SpeechRecognition Cordova Speech Recognition Plugin Apache diff --git a/src/android/SpeechRecognition.java b/src/android/SpeechRecognition.java index 6787d44..44ebf99 100644 --- a/src/android/SpeechRecognition.java +++ b/src/android/SpeechRecognition.java @@ -65,7 +65,7 @@ else if (ACTION_SPEECH_RECOGNIZE_START.equals(action)) { } String lang = args.optString(0, "en"); - + boolean partial = args.optBoolean(1,false); this.speechRecognizerCallbackContext = callbackContext; final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); @@ -75,6 +75,8 @@ else if (ACTION_SPEECH_RECOGNIZE_START.equals(action)) { intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true); + Handler loopHandler = new Handler(Looper.getMainLooper()); loopHandler.post(new Runnable() { @@ -124,7 +126,7 @@ private boolean DoInit() { return this.recognizerPresent; } - private void fireRecognitionEvent(ArrayList transcripts, float[] confidences) { + private void fireRecognitionEvent(ArrayList transcripts, float[] confidences,String recognitionType) { JSONObject event = new JSONObject(); JSONArray results = new JSONArray(); try { @@ -139,7 +141,10 @@ private void fireRecognitionEvent(ArrayList transcripts, float[] confide alternatives.put(result); results.put(alternatives); } - event.put("type", "result"); + if(recognitionType.equals("final-result")) + event.put("type", "result"); + else if (recognitionType.equals("partial-result")) + event.put("type", "partial-result"); event.put("emma", null); event.put("interpretation", null); event.put("results", results); @@ -205,7 +210,8 @@ public void onError(int error) { Log.d(LOG_TAG, "error speech"); if (listening) { fireErrorEvent(); - fireEvent("end"); + //fireEvent("end"); + fireEvent("error"); } listening = false; } @@ -218,16 +224,29 @@ public void onEvent(int eventType, Bundle params) { @Override public void onPartialResults(Bundle partialResults) { Log.d(LOG_TAG, "partial results"); + + Log.d(LOG_TAG, "results"); + String str = new String(); + Log.d(LOG_TAG, "onPartialResults " + partialResults); + ArrayList transcript = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); + float[] confidence = partialResults.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES); + if (transcript.size() > 0) { + Log.d(LOG_TAG, "fire recognition event"); + fireRecognitionEvent(transcript, confidence,"partial-result"); + } + } @Override public void onReadyForSpeech(Bundle params) { Log.d(LOG_TAG, "ready for speech"); listening = true; + fireEvent("ready"); } @Override public void onResults(Bundle results) { + Log.d(LOG_TAG, "results"); String str = new String(); Log.d(LOG_TAG, "onResults " + results); @@ -235,7 +254,7 @@ public void onResults(Bundle results) { float[] confidence = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES); if (transcript.size() > 0) { Log.d(LOG_TAG, "fire recognition event"); - fireRecognitionEvent(transcript, confidence); + fireRecognitionEvent(transcript, confidence,"final-result"); } else { Log.d(LOG_TAG, "fire no match event"); fireEvent("nomatch"); diff --git a/www/SpeechRecognition.js b/www/SpeechRecognition.js index 0ea6061..fdcdd36 100644 --- a/www/SpeechRecognition.js +++ b/www/SpeechRecognition.js @@ -15,6 +15,7 @@ var SpeechRecognition = function () { this.interimResults = false; this.maxAlternatives = 1; this.serviceURI = ""; + this.partialResult = false; // event methods this.onaudiostart = null; @@ -28,6 +29,8 @@ var SpeechRecognition = function () { this.onerror = null; this.onstart = null; this.onend = null; + this.onpartial = null; + this.onready = null; exec(function() { console.log("initialized"); @@ -57,6 +60,12 @@ SpeechRecognition.prototype.start = function() { that.onnomatch(event); } else if (event.type === "start" && typeof that.onstart === "function") { that.onstart(event); + } else if (event.type === "ready" && typeof that.onready === "function") { + that.onready(event); + } else if (event.type === "partial-result" && typeof that.onpartial === "function") { + that.onpartial(event); + }else if (event.type === "error" && typeof that.onerror === "function") { + that.onerror(event); } else if (event.type === "end" && typeof that.onend === "function") { that.onend(event); } @@ -67,7 +76,7 @@ SpeechRecognition.prototype.start = function() { } }; - exec(successCallback, errorCallback, "SpeechRecognition", "start", [this.lang]); + exec(successCallback, errorCallback, "SpeechRecognition", "start", [this.lang,this.partialResult]); }; SpeechRecognition.prototype.stop = function() {