Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.apache.cordova.speech.speechrecognition"
version="0.1.1">
version="0.1.2">
<name>SpeechRecognition</name>
<description>Cordova Speech Recognition Plugin</description>
<license>Apache</license>
Expand Down
29 changes: 24 additions & 5 deletions src/android/SpeechRecognition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {

Expand Down Expand Up @@ -124,7 +126,7 @@ private boolean DoInit() {
return this.recognizerPresent;
}

private void fireRecognitionEvent(ArrayList<String> transcripts, float[] confidences) {
private void fireRecognitionEvent(ArrayList<String> transcripts, float[] confidences,String recognitionType) {
JSONObject event = new JSONObject();
JSONArray results = new JSONArray();
try {
Expand All @@ -139,7 +141,10 @@ private void fireRecognitionEvent(ArrayList<String> 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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -218,24 +224,37 @@ 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<String> 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);
ArrayList<String> transcript = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
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");
Expand Down
11 changes: 10 additions & 1 deletion www/SpeechRecognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var SpeechRecognition = function () {
this.interimResults = false;
this.maxAlternatives = 1;
this.serviceURI = "";
this.partialResult = false;

// event methods
this.onaudiostart = null;
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand All @@ -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() {
Expand Down