I'm building application that listens for keyword in service for this I'm using sphinx, I noticed that when I speak keyword the onPartialResult method runs again and again and never able to run onresult so that I could do some work, I dnt know about how to configure assets etc, I just downloaded project, Here is my code:
privatevoidrunRecognizerSetup(){//Recognizerinitializationisatime-consuminganditinvolvesIO,//soweexecuteitinasynctasknewAsyncTask<Void,Void,Exception>(){@OverrideprotectedExceptiondoInBackground(Void...params){try{Assetsassets=newAssets(VoiceService.this);FileassetDir=assets.syncAssets();setupRecognizer(assetDir);}catch(IOExceptione){returne;}returnnull;}@OverrideprotectedvoidonPostExecute(Exceptione){if(e!=null){Log.i(LOG_TAG,"Failed to init recognizer ");}else{startListening(KWS_SEARCH);}}}.execute();}privatevoidstartListening(StringkwsSearch){recognizer.startListening(KWS_SEARCH);Log.i(LOG_TAG,"startedlistening");}@OverridepublicvoidonDestroy(){super.onDestroy();if(recognizer!=null){recognizer.cancel();recognizer.shutdown();}}@OverridepublicvoidonPartialResult(Hypothesishypothesis){if(hypothesis==null)return;Stringtext=hypothesis.getHypstr();if(text.contains(KEYPHRASE)){Toast.makeText(this,"onPartialResult text="+text,Toast.LENGTH_SHORT).show();switchSearch(KWS_SEARCH);}Log.i(LOG_TAG,"onPartialResult text="+text);}/** * This callback is called when we stop the recognizer. */@OverridepublicvoidonResult(Hypothesishypothesis){if(hypothesis!=null){Stringtext=hypothesis.getHypstr();Log.i(LOG_TAG,"onResult text="+text);IntentIntent=newIntent(this,ReceiverActivity.class);Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(Intent);}}@OverridepublicvoidonBeginningOfSpeech(){Log.i(LOG_TAG,"onBeginningOfSpeech");this.timer.schedule(newTimerTask(){@Overridepublicvoidrun(){recognizer.stop();Log.i("Runningafter","runningafter2seco");}},5000);this.timer.schedule(newTimerTask(){@Overridepublicvoidrun(){recognizer.startListening(KWS_SEARCH);Log.i("startedlisteningagain","startedlisteningagain");}},5000);}@OverridepublicvoidonEndOfSpeech(){if(!recognizer.getSearchName().contains(KWS_SEARCH))//switchSearch(KWS_SEARCH);Log.i(LOG_TAG,"onEndOfSpeech");}privatevoidswitchSearch(StringsearchName){//Log.i(LOG_TAG,"switchSearch searchName = "+searchName);if(recognizer!=null){Log.i("RecognizerNull ","Null");recognizer.stop();recognizer.startListening(searchName);newjava.util.Timer().schedule(newjava.util.TimerTask(){@Overridepublicvoidrun(){}},2000);this.timer.schedule(newTimerTask(){@Overridepublicvoidrun(){recognizer.startListening(KWS_SEARCH);Log.i("Runningafter","runningafter2seco");}},10000);}}privatevoidsetupRecognizer(FileassetsDir)throwsIOException{//Therecognizercanbeconfiguredtoperformmultiplesearches//ofdifferentkindandswitchbetweenthemrecognizer=SpeechRecognizerSetup.defaultSetup().setAcousticModel(newFile(assetsDir,"en-us-ptm")).setDictionary(newFile(assetsDir,"cmudict-en-us.dict")).setRawLogDir(assetsDir)//Todisableloggingofrawaudiocommentoutthiscall(takesalotofspaceonthedevice).setKeywordThreshold(1e-45f)//Thresholdtotuneforkeyphrasetobalancebetweenfalsealarmsandmisses.setBoolean("-allphone_ci",true)//Usecontext-independentphoneticsearch,context-dependentistooslowformobile.getRecognizer();//Createkeyword-activationsearch.recognizer.addKeyphraseSearch(KWS_SEARCH,KEYPHRASE);recognizer.addListener(this);Log.i(LOG_TAG,"setupRecognizer");}@OverridepublicvoidonError(Exceptionerror){Log.i(LOG_TAG,"onError "+error.getMessage());}@OverridepublicvoidonTimeout(){switchSearch(KWS_SEARCH);Log.i(LOG_TAG,"onTimeout");}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm building application that listens for keyword in service for this I'm using sphinx, I noticed that when I speak keyword the onPartialResult method runs again and again and never able to run onresult so that I could do some work, I dnt know about how to configure assets etc, I just downloaded project, Here is my code:
You need to stop listening with
recognizer.cancel()immediately after recognizer detected a keyphrase to reset partial result.you mean in onbegining method?
in
onPartialResult. Actually the original demo did it right. You modified switchSearch with a timer, you should have return that modification back.In onpartial method I first stop recognizer and then in the next line started recognizer but in this case it recognizes only once
Could you tell me what is threshold for keyword?