Wednesday, 15 January 2014

Android Speech Recognition as a service on Android 4.1 & 4.2 -



Android Speech Recognition as a service on Android 4.1 & 4.2 -

i have managed continuous speech recognition working (using speechrecognizer class) service on android versions 4.1. question concerns getting working on versions 4.1 , 4.2 known there problem in api doesn't documented in few seconds after voice recognition started, if no voice input has been detected it's if speech recogniser dies silently. (http://code.google.com/p/android/issues/detail?id=37883)

i have found question proposes work-around problem (voice recognition stops listening after few seconds), unsure how implement handler required solution. aware of 'beep' happen every few seconds workaround cause, getting continuous voice recognition more of import me.

if has other alternative workarounds i'd hear too.

this work around android version 4.1.1.

public class myservice 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; private boolean misstreamsolo; static final int msg_recognizer_start_listening = 1; static final int msg_recognizer_cancel = 2; @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()); } protected static class incominghandler extends handler { private weakreference<myservice> mtarget; incominghandler(myservice target) { mtarget = new weakreference<myservice>(target); } @override public void handlemessage(message msg) { final myservice target = mtarget.get(); switch (msg.what) { case msg_recognizer_start_listening: if (build.version.sdk_int >= build.version_codes.jelly_bean) { // turn off beep sound if (!misstreamsolo) { maudiomanager.setstreamsolo(audiomanager.stream_voice_call, true); misstreamsolo = 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: if (misstreamsolo) { maudiomanager.setstreamsolo(audiomanager.stream_voice_call, false); misstreamsolo = false; } target.mspeechrecognizer.cancel(); target.mislistening = false; //log.d(tag, "message canceled recognizer"); //$non-nls-1$ break; } } } // count downwards 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); seek { mservermessenger.send(message); message = message.obtain(null, msg_recognizer_start_listening); mservermessenger.send(message); } grab (remoteexception e) { } } }; @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 downwards anymore if (miscountdownon) { miscountdownon = false; mnospeechcountdown.cancel(); } //log.d(tag, "onbeginingofspeech"); //$non-nls-1$ } @override public void onbufferreceived(byte[] buffer) { } @override public void onendofspeech() { //log.d(tag, "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); seek { mservermessenger.send(message); } grab (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 >= build.version_codes.jelly_bean) { miscountdownon = true; mnospeechcountdown.start(); } log.d(tag, "onreadyforspeech"); //$non-nls-1$ } @override public void onresults(bundle results) { //log.d(tag, "onresults"); //$non-nls-1$ } @override public void onrmschanged(float rmsdb) { } } }

02/16/2013 - prepare beep sound if utilize text speech in app create sure turn off solo stream in onresults

android speech-recognition android-4.2-jelly-bean

No comments:

Post a Comment