Running Android Speech Recognition as Service: will not start -
i'm using solution here: android speech recognition service on android 4.1 & 4.2 code below gets onstartcommand() method, speech recogntion never seems kick off, evidenced fact onreadyforspeech() method never called.
update: added , allowed onreadyforspeech() called , onerror() called error code: 6 after onreadyforspeech() method complete (this goes continuous loop because start listening code started again after onerror() called). hoan nguyen states below, error code 6 error_speech_timeout never see speech recognition dialog come know start speaking. tried start speaking after kicking off code without dialog coming , still error code 6, doing wrong here?
update (9-06-13): hoan stated, there no dialog comes up. start talking listening called. ran issue trying restart listener after every onresult() called...that issue , complete code can found here: android speech speech recognition: repeated calling of speechrecognizer.startlistening() fails on jb 4.1.2
my activity code calls service:
startservice(new intent(voicerecognition.this, voicerecogservice.class));
my service:
public class voicerecogservice extends service { protected audiomanager maudiomanager; protected speechrecognizer mspeechrecognizer; protected intent mspeechrecognizerintent; protected final messenger mservermessenger = new messenger(new incominghandler(this)); protected boolean mislistening; protected volatile boolean miscountdownon; static final int msg_recognizer_start_listening = 1; static final int msg_recognizer_cancel = 2; private int mbindflag; private messenger mservicemessenger; @override public void oncreate() { super.oncreate(); maudiomanager = (audiomanager) getsystemservice(context.audio_service); mspeechrecognizer = speechrecognizer.createspeechrecognizer(this); mspeechrecognizer.setrecognitionlistener(new speechrecognitionlistener()); mspeechrecognizerintent = new intent(recognizerintent.action_recognize_speech); mspeechrecognizerintent.putextra(recognizerintent.extra_language_model, recognizerintent.language_model_free_form); mspeechrecognizerintent.putextra(recognizerintent.extra_calling_package, this.getpackagename()); //mspeechrecognizer.startlistening(mspeechrecognizerintent); } protected static class incominghandler extends handler { private weakreference<voicerecogservice> mtarget; incominghandler(voicerecogservice target) { mtarget = new weakreference<voicerecogservice>(target); } @override public void handlemessage(message msg) { final voicerecogservice target = mtarget.get(); switch (msg.what) { case msg_recognizer_start_listening: if (build.version.sdk_int >= 16);//build.version_codes.jelly_bean) { // turn off beep sound target.maudiomanager.setstreammute(audiomanager.stream_system, true); } if (!target.mislistening) { target.mspeechrecognizer.startlistening(target.mspeechrecognizerintent); target.mislistening = true; //log.d(tag, "message start listening"); //$non-nls-1$ } break; case msg_recognizer_cancel: target.mspeechrecognizer.cancel(); target.mislistening = false; //log.d(tag, "message canceled recognizer"); //$non-nls-1$ break; } } } // count down timer jelly bean work around protected countdowntimer mnospeechcountdown = new countdowntimer(5000, 5000) { @override public void ontick(long millisuntilfinished) { // todo auto-generated method stub } @override public void onfinish() { miscountdownon = false; message message = message.obtain(null, msg_recognizer_cancel); try { mservermessenger.send(message); message = message.obtain(null, msg_recognizer_start_listening); mservermessenger.send(message); } catch (remoteexception e) { } } }; @override public int onstartcommand (intent intent, int flags, int startid) { //mspeechrecognizer.startlistening(mspeechrecognizerintent); try { message msg = new message(); msg.what = msg_recognizer_start_listening; mservermessenger.send(msg); } catch (remoteexception e) { } return start_not_sticky; } @override public void ondestroy() { super.ondestroy(); if (miscountdownon) { mnospeechcountdown.cancel(); } if (mspeechrecognizer != null) { mspeechrecognizer.destroy(); } } protected class speechrecognitionlistener implements recognitionlistener { @override public void onbeginningofspeech() { // speech input processed, there no need count down anymore if (miscountdownon) { miscountdownon = false; mnospeechcountdown.cancel(); } //log.d(tag, "onbeginingofspeech"); //$non-nls-1$ } @override public void onbufferreceived(byte[] buffer) { string stest = ""; } @override public void onendofspeech() { log.d("testing: speech service", "onendofspeech"); //$non-nls-1$ } @override public void onerror(int error) { if (miscountdownon) { miscountdownon = false; mnospeechcountdown.cancel(); } mislistening = false; message message = message.obtain(null, msg_recognizer_start_listening); try { mservermessenger.send(message); } catch (remoteexception e) { } //log.d(tag, "error = " + error); //$non-nls-1$ } @override public void onevent(int eventtype, bundle params) { } @override public void onpartialresults(bundle partialresults) { } @override public void onreadyforspeech(bundle params) { if (build.version.sdk_int >= 16);//build.version_codes.jelly_bean) { miscountdownon = true; mnospeechcountdown.start(); maudiomanager.setstreammute(audiomanager.stream_system, false); } log.d("testing: speech service", "onreadyforspeech"); //$non-nls-1$ } @override public void onresults(bundle results) { //log.d(tag, "onresults"); //$non-nls-1$ } @override public void onrmschanged(float rmsdb) { } } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } }
Comments
Post a Comment