How do I use Android SpeechRecognizer as a service? -
i trying run android voice recognition service. can verify oncreate() , onstart() methods of service called, no callbacks speech recognition methods called, despite fact have set speechrecognizer object correctly. speech recognition seems work when done in activity instead of service. how make work service? manifest issue?
package net.viralpatel.android.speechtotextdemo; import java.io.bufferedwriter; import java.io.filewriter; import java.io.ioexception; import java.util.arraylist; import android.app.service; import android.content.intent; import android.os.bundle; import android.os.ibinder; import android.speech.recognitionlistener; import android.speech.recognizerintent; import android.speech.speechrecognizer; import android.util.log; import android.widget.toast; public class myservice extends service implements recognitionlistener { private speechrecognizer speechrecognizer; @override public ibinder onbind(intent arg0) { return null; } @override public void oncreate() { toast.maketext(this, "my service created", toast.length_long).show(); log.d("tag", "oncreate"); speechrecognizer = speechrecognizer.createspeechrecognizer(getapplicationcontext()); speechrecognizer.setrecognitionlistener(this); intent intent = new intent(recognizerintent.action_recognize_speech); intent.putextra(recognizerintent.extra_language_model, recognizerintent.language_model_web_search); speechrecognizer.startlistening(intent); } @override public void ondestroy() { toast.maketext(this, "my service stopped", toast.length_long).show(); log.d("tag", "ondestroy"); } @override public void onstart(intent intent, int startid) { toast.maketext(this, "my service started", toast.length_long).show(); log.d("tag", "onstart"); } @override public void onbeginningofspeech() { log.d("speech", "onbeginningofspeech"); } @override public void onbufferreceived(byte[] buffer) { log.d("speech", "onbufferreceived"); } @override public void onendofspeech() { log.d("speech", "onendofspeech"); } @override public void onerror(int error) { log.d("speech", "onerror"); } @override public void onevent(int eventtype, bundle params) { log.d("speech", "onevent"); } @override public void onpartialresults(bundle partialresults) { log.d("speech", "onpartialresults"); } @override public void onreadyforspeech(bundle params) { log.d("speech", "onreadyforspeech"); } @override public void onresults(bundle results) { log.d("speech", "onresults"); arraylist strlist = results.getstringarraylist(speechrecognizer.results_recognition); (int = 0; < strlist.size();i++ ) { log.d("speech", "result=" + strlist.get(i)); } bufferedwriter out; try { out = new bufferedwriter(new filewriter("mnt/sdcard/results.txt")); // out.write(processor.execute(strlist.get(0).tostring())); out.write("hello world"); } catch (ioexception e) { log.e("speech",e.tostring()); } } @override public void onrmschanged(float rmsdb) { log.d("speech", "onrmschanged"); } }
there 2 things think need clarify , may provide workaround.
have declared service in manifest properly? believe addressed.
speech recognition may not start "oncreate" of service. had done similar implementation didn't work. can try placing startlistening(intent) in other method , call explicitly. worked me.
let me know if helps.
Comments
Post a Comment