Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - Voice Recognition stops listening after a few seconds

I tried a lot but can′t find it out, so I hope you can help me.

I am trying to build my own voice recognition app, which doesn′t show up the dialog.

I already wrote some code and it works quite fine, but my problem is that the recognizer seems to stop without any errors or other messanges in the LogCat.

A strange fact is that the "onRmsChanged" from the "RecognitionListener" interface is still called all the time, but no "onBeginningOfSpeech" is called anymore.

If I speak just after the speech recognition has started it works. But it doesn′t if I wait a few seconds.

The used API is 4.0.3 and I installed it on my Nexus 7 with the Version 4.2.1

I would really appreciate if you have some good ideas.

Some code snippets:

My class:

class SpeechListener implements RecognitionListener
{

    public void onBeginningOfSpeech() 
    {
        Log.d(TAG, "onBeginningOfSpeech()");            
    }

    public void onBufferReceived(byte[] buffer) 
    {
        Log.d(TAG, "onBufferReceived()");
    }

    public void onEndOfSpeech() 
    {
        Log.d(TAG, "onEndOfSpeech()");

    }

    public void onError(int error) 
    {
        Log.d(TAG, "onError(): " + error);

        if(error == SpeechRecognizer.ERROR_NO_MATCH)
        {

        }
        else if(error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)
        {

        }
        else
        {
            tvOutput.setText("Error: " + error);    
        }

    }

    public void onEvent(int eventType, Bundle params) 
    {
        Log.d(TAG, "onEvent()");
    }

    public void onPartialResults(Bundle partialResults) 
    {
        Log.d(TAG, "onPartialResults()");
    }

    public void onReadyForSpeech(Bundle params) 
    {
        Log.d(TAG, "onReadyForSpeech()");
    }

    public void onResults(Bundle results) 
    {
        Log.d(TAG, "onResults(): " + results);

        String str = new String();

        ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        for(int i = 0; i < data.size(); i++)
        {
            str += data.get(i) + "
";

        }           
        tvOutput.setText(tvOutput.getText().toString() + "

" + "Results: " + str);           

    }
    public void onRmsChanged(float rmsdB) 
    {
        Log.d(TAG, "onRmsChanged()");
    }

}

And my implementation in the MainActivity:

    this.srSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    this.srSpeechRecognizer.setRecognitionListener(new SpeechListener());

    this.iSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    this.iSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    this.iSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");      
    this.iSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);

And so it′s started:

srSpeechRecognizer.startListening(iSpeechIntent);

Logs with speaking:

    12-16 13:50:53.576: D/DreamManagerService(485): Dream finished:   android.os.Binder@415bbf38
    12-16 13:50:53.576: I/DreamManagerService(485): Leaving dreamland.
    12-16 13:50:53.576: I/DreamController(485): Stopping dream: name=ComponentInfo{com.google.android.deskclock/com.android.deskclock.Screensaver}, isTest=false, userId=0
    12-16 13:50:53.586: I/PowerManagerService(485): Waking up from dream...
    12-16 13:50:53.616: I/ActivityManager(485): No longer want com.google.android.gsf.login (pid 13171): empty #17
    12-16 13:50:56.796: I/GoogleRecognitionServiceImpl(1461): #startListening [de-DE]
    12-16 13:50:56.806: I/ActivityManager(485): Start proc com.google.android.gsf.login for service com.google.android.gsf.login/com.google.android.gsf.loginservice.GoogleLoginService: pid=13343 uid=10019 gids={50019, 3003, 1007, 1028, 1015, 2001, 3006}
    12-16 13:50:56.866: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:56.886: D/dalvikvm(1461): GC_FOR_ALLOC freed 516K, 12% free 8706K/9892K, paused 18ms, total 18ms
    12-16 13:50:56.906: D/dalvikvm(1461): GC_CONCURRENT freed 160K, 9% free 9015K/9892K, paused 3ms+2ms, total 21ms
    12-16 13:50:56.906: I/AudioService(485):  AudioFocus  requestAudioFocus() from android.media.AudioManager@4135e960com.google.android.speech.audio.AudioController$1@41261910
    12-16 13:50:56.916: I/VS.G3EngineManager(1461): create_rm: m=ENDPOINTER_VOICESEARCH,l=en-US
    12-16 13:50:56.916: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:56.916: I/VS.G3EngineManager(1461): Brought up new g3 instance :/system/usr/srec/en-US/endpointer_voicesearch.config for: en-USin: 3 ms
    12-16 13:50:56.926: I/ConnectionFactoryImpl(1461): Opening SSL connection: vs.google.com:14259
    12-16 13:50:56.966: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.016: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.066: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.116: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.166: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.216: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.266: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.316: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.366: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.416: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.466: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.516: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.566: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.616: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.666: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.716: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.766: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.816: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.866: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.916: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:57.966: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.016: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.066: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.116: I/MainActivity/SpeechListener(13268): onBeginningOfSpeech()
    12-16 13:50:58.126: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.176: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.226: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.276: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.326: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.376: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.426: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.476: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.526: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.576: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.626: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.676: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.726: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.776: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.826: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.876: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.926: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:58.976: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.026: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.076: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.126: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.176: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.236: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.286: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.336: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.386: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.436: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.486: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.536: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.586: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.636: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.646: I/MicrophoneInputStream(1461): mic_close
    12-16 13:50:59.666: I/AudioService(485):  AudioFocus  abandonAudioFocus() from         android.media.AudioManager@4135e960com.google.android.speech.audio.AudioController$1@41261910
    12-16 13:50:59.666: D/dalvikvm(1461): threadid=37: thread exiting, not yet detached (count=0)
    12-16 13:50:59.666: I/MainActivity/SpeechListener(13268): onEndOfSpeech()
    12-16 13:50:59.676: I/decoder(1461): INFO: recognition time wall: 2.732 sec user: 0.54 sec sys: 0.08 sec 
    12-16 13:50:59.686: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.736: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.786: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.826: I/MainActivity/SpeechListener(13268): onResults(): Bundle[mParcelledData.dataSize=292]
    12-16 13:50:59.836: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.886: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.936: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:50:59.986: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:51:00.046: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:51:00.096: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:51:00.146: D/MainActivity/SpeechListener(13268): onRmsChanged()
    12-16 13:51:00.196: D/MainActivity/SpeechListener(13268): onRmsChanged()

Logs without speaking:

    12-16 13:53:39.246: I/GoogleRecognitionServiceImpl(1461): #startListening [de-DE]
    12-16 13:53:39.296: D/dalvikvm(1461): GC_FOR_ALLOC freed 567K, 12% free 8708K/9892K, paused 21ms, total 21ms
    12-16 13:53:39.316: D/dalvikvm(1461): GC_CONCURRENT freed 164K, 9% free 9017K/9892K, paused 3ms+2ms, total 21ms
    12-16 13:53:39.316: D/MainActivity/SpeechListener(13441): onRmsChanged()
    12-16 13:53:39.316: I/AudioService(485):  AudioFocus  requestAudioFocus() from         android.media.AudioManager@4135e960com.google.android.speech.audio.AudioController$1@41261910
    12-16 13:53:39.326: I/VS.G3EngineManager(1461): create_rm: m=ENDPOINTER_VOICESEARCH,l=en-US
    12-16 13:53:39.326: I/ConnectionFactoryImpl(1461): Opening SSL connection: vs.google.com:14259
    12-16 13:53:39.326: I/VS.G3EngineManager(1461): Brought up new g3 instance :/system/usr/srec/en-US/endpointer_voicesearch.config for: en-USin: 5 ms
    12-16 13:53:39.366: D/MainActivity/SpeechListener(13441): onRmsChanged()
    12-16 13:53:39.416: D/MainActivity/SpeechListen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The only solution that will for sure get around this issue is to use a 3rd party service. 4.1.1 and 4.2 both rely on a version of the speech recognition service that does not adhere to the documented behavior in that the service running it dies silently.

If you do not wish to use a 3rd party API, and you need to account for this service death in some manner, it is possible but it's not pretty or ideal.

Once the service dies, none of the following methods will ever be called :

  • onBeginningOfSpeech
  • onError
  • onResults
  • onEndOfSpeech

But if onBeginningOfSpeech is called before the service dies, you can be assured that either onError or onEndOfSpeech will eventually be called.

Therefore, if all you want is to be sure you are made aware of the life and death of the service in Jellybean the workaround for this problem in the built-in SpeechRecognizer is to do the following:

  • Create a boolean flag like isSpeechRecognizerAlive.
  • Any time you start up the SpeechRecognizer, set the above flag to false.
  • In onBeginningOfSpeech, if it is called, set isSpeechRecognizerAlive to true.
  • Maintain a Handler that, on a 4 second delay will check the status of isSpeechRecognizerAlive. If it is false, manually kill the SpeechRecognizer instance. If is is true, do nothing. The normal flow will take care of things for you.

Why this is not an ideal solution to maintain a continuous speech recognition setup

It wasn't directly stated in your question but a few people want to do this so they can have continuous speech recognition. This is not really a good way to do that in 4.1.1 and 4.2 because Google's SpeechRecognition service now kicks off with a non-optional "bloop" sound effect. There appears to be no way to turn this sound off. Nothing is listed in the API to control it. Your users WILL NOT appreciate being "blooped" at on a 4 second repeating loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...