SpeechRecognizer causes ANR… I need help with Android speech API

后端 未结 6 1060
一生所求
一生所求 2020-12-01 02:14

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.

相关标签:
6条回答
  • 2020-12-01 02:53

    You shouldn't need to create the SpeechRecognizer class yourself, nor do you need to implement a RecognizerListener. Google made them public to be nice, but they look pretty complicated and probably for use by experts only.

    To get text from the users speech yo simply need to use the RecognizerIntent.ACTION_RECOGNIZE_SPEECH to launch the built-in speech recognizer Activity and then wait for the result to come back in onActivityResult. Take a look at the example code here:

    http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

    I pulled it from this article.

    http://developer.android.com/resources/articles/speech-input.html

    Good Luck.

    0 讨论(0)
  • 2020-12-01 03:04

    Make Sure to use the RECORD_AUDIO permission.

    0 讨论(0)
  • 2020-12-01 03:07

    I got this to work by installing the Google Voice App

    0 讨论(0)
  • 2020-12-01 03:08

    From a service, you have to create the recognizer from looper running on the main thread. Also RecognizerIntent.EXTRA_RESULTS should be SpeechRecognizer.RESULTS_RECOGNITION.

    psuedo code:

    public class VoiceRecognition implements RecognitionListener, Runnable 
    {
        @Override
        public void run()
        {
            recognizer = SpeechRecognizer.createSpeechRecognizer(yourContext);
            recognizer.setRecognitionListener((RecognitionListener) this);
    
             intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
             //... all the intent stuff ...
             intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
             recognizer.startListening(intent);
        }
        @Override
        public void onResults(Bundle results)
        {
           ArrayList<String> matches;
           matches=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        }
     }
    
    ....
    VoiceRecognition voiceRecognizer = new VoiceRecognition();
    Handler loopHandler = new Handler(Looper.getMainLooper());
    loopHandler.post(voiceRecognizer);
    
    0 讨论(0)
  • 2020-12-01 03:09

    Instead of getApplicationContext(), use this (i.e. your service instance). android.app.Service inherits from Context.

    0 讨论(0)
  • 2020-12-01 03:09

    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.

    0 讨论(0)
提交回复
热议问题