Alex Rivera | Logout

SpeechRecognizer causes ANR... I need help with Android speech API

Asked 2010-12-30T04:09:53.227
12

EDIT: I should have mentioned this already, but I'm running this code in a service. The entire app is turned on/off by a widget button and has no activity.


Update: I tried attaching the SDK sources to the project so I could get a more precise idea of where the failure was occurring, but from the looks of it, only public APIs are included, which seems to make them a lot less useful... can anyone suggest at least a debugging approach for solving this issue? I'm kind of stuck.


I'm trying to use Android's speech recognition package to record user speech and translate it to text. Unfortunately, when I attempt initiate listening, I get an ANR error that doesn't point to anything specific.

As the SpeechRecognizer API indicates, a RuntimeException is thrown if you attempt to call it from the main thread. This would make me wonder if the processing was just too demanding... but I know that other applications use the Android API for this purpose and it is typically pretty snappy.

java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread

Here is a (trimmed) sample of the code I'm trying to call from my service. Is this the proper approach?

Thanks for taking the time to help. This has been a hurdle I haven't been able to get over yet.

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        "com.domain.app");

SpeechRecognizer recognizer = SpeechRecognizer
        .createSpeec
Edit
Report

2 Answers

5

Make Sure to use the RECORD_AUDIO permission.

answered 2011-12-29T20:23:19.190
2

Here is the problem I ran into, which might be related to yours. I was getting null, no matter what, from this line:

  ArrayList<String> voiceResults = results                .getStringArrayList(RecognizerIntent.EXTRA_RESULTS);  

Changing it to this completely fixed the problem:

  ArrayList<String> voiceResults = results                .getStringArrayList("results_recognition");  

I printed out the keyset from the bundle, and that was the only value. Note that the value of "results_recognition" is not the value of the string stored at RecognizerIntent.EXTRA_RESULTS. I think that's a bug, or at least it should be, since using RecognizerIntent.EXTRA_RESULTS works fine in the Activity way of doing speech recognition, but not when someone uses SpeechRecognizer.

Anyways, i most likely won't check this website again, but please email me if you have any questions, since I now do have speech recognition working without any prompts, beeps, or anything else.

answered 2011-04-13T22:17:28.750

Your Answer