i developing speech recognition system in android . have succeeded in . google voice dialog box keeps annoying me , want rid off . i.e want run voice recognition in background service . know there many posts on answers . problem every code here on native java , develop on phone gap . not able port thing phone gap . have code below . , appreciated ! , use this plugin on phone gap . in advance .
package com.phonegap.plugins.speech; import java.util.arraylist; import java.util.locale; import org.json.jsonarray; import org.apache.cordova.cordovaplugin; import org.apache.cordova.callbackcontext; import android.util.log; import android.app.activity; import android.content.intent; import android.speech.recognizerintent; /** * style , such borrowed tts , phonelistener plugins */ public class speechrecognizer extends cordovaplugin { private static final string log_tag = speechrecognizer.class.getsimplename(); private static int request_code = 1001; private callbackcontext callbackcontext; private languagedetailschecker languagedetailschecker; //@override public boolean execute(string action, jsonarray args, callbackcontext callbackcontext) { boolean isvalidaction = true; this.callbackcontext= callbackcontext; // action selector if ("startrecognize".equals(action)) { // recognize speech startspeechrecognitionactivity(args); } else if ("getsupportedlanguages".equals(action)) { getsupportedlanguages(); } else { // invalid action this.callbackcontext.error("unknown action: " + action); isvalidaction = false; } return isvalidaction; } // list of supported languages private void getsupportedlanguages() { if (languagedetailschecker == null){ languagedetailschecker = new languagedetailschecker(callbackcontext); } // create , launch languages intent intent detailsintent = new intent(recognizerintent.action_get_language_details); cordova.getactivity().sendorderedbroadcast(detailsintent, null, languagedetailschecker, null, activity.result_ok, null, null); } /** * fire intent start speech recognition activity. * * @param args argument array following string args: [req code][number of matches][prompt string] */ private void startspeechrecognitionactivity(jsonarray args) { int maxmatches = 0; string prompt = ""; string language = locale.getdefault().tostring(); try { if (args.length() > 0) { // maximum number of matches, 0 means recognizer decides string temp = args.getstring(0); maxmatches = integer.parseint(temp); } if (args.length() > 1) { // optional text prompt prompt = args.getstring(1); } if (args.length() > 2) { // optional language specified language = args.getstring(2); } } catch (exception e) { log.e(log_tag, string.format("startspeechrecognitionactivity exception: %s", e.tostring())); } // create intent , set parameters intent intent = new intent(recognizerintent.action_recognize_speech); intent.putextra(recognizerintent.extra_language_model, recognizerintent.language_model_free_form); intent.putextra(recognizerintent.extra_language, language); if (maxmatches > 0) intent.putextra(recognizerintent.extra_max_results, maxmatches); if (!prompt.equals("")) intent.putextra(recognizerintent.extra_prompt, prompt); cordova.startactivityforresult(this, intent, request_code); } /** * handle results recognition activity. */ @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == activity.result_ok) { // fill list view strings recognizer thought have heard arraylist<string> matches = data.getstringarraylistextra(recognizerintent.extra_results); returnspeechresults(matches); } else { // failure - let caller know this.callbackcontext.error(integer.tostring(resultcode)); } super.onactivityresult(requestcode, resultcode, data); } private void returnspeechresults(arraylist<string> matches) { jsonarray jsonmatches = new jsonarray(matches); this.callbackcontext.success(jsonmatches); } }
because need little different functionality provided plugin, seems need write own plugin. can of course use plugin sources (especially plugin.xml, speechrecognizer.js , speechrecognizer.java) example how implement in general.
Comments
Post a Comment