gotev / android-speech

Android speech recognition and text to speech made easy
http://gotev.github.io/android-speech/
Apache License 2.0
477 stars 156 forks source link

SpeechRecognitionException: 3 - Audio recording error #16

Closed uzuki-P closed 6 years ago

uzuki-P commented 6 years ago

I got an error that the speech just decided to finish it self. The log error not even showing if I'm not adding Logger.setLogLevel(Logger.LogLevel.DEBUG);

Logcat with 'speech' regex:

10-06 23:24:06.517 9162-9162/skrip.si.findthissong D/Speech: DelayedOperation - created delayed operation with tag: delayStopListening
10-06 23:24:06.566 9162-9162/skrip.si.findthissong I/TextToSpeech: Sucessfully bound to com.google.android.tts
10-06 23:24:06.567 9162-9162/skrip.si.findthissong W/TextToSpeech: setLanguage failed: not bound to TTS engine
10-06 23:24:06.567 9162-9162/skrip.si.findthissong D/Speech: DelayedOperation - created delayed operation with tag: delayStopListening
10-06 23:24:06.572 9162-9162/skrip.si.findthissong I/speech: speech recognition is now active
10-06 23:24:06.576 9162-9215/skrip.si.findthissong D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-7656231050442859303, firebase_screen_class(_sc)=SpeechSearchDialog, firebase_screen_id(_si)=-7656231050442859302}]
10-06 23:24:06.650 9162-9162/skrip.si.findthissong I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
10-06 23:24:06.660 9162-9242/skrip.si.findthissong I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
10-06 23:24:06.688 9162-9162/skrip.si.findthissong I/Speech: Speech - TextToSpeech engine successfully started
10-06 23:24:06.763 9162-9162/skrip.si.findthissong D/speech: rms is now: -2.12
10-06 23:24:06.821 9162-9162/skrip.si.findthissong D/speech: rms is now: -2.12
10-06 23:24:06.822 9162-9162/skrip.si.findthissong E/Speech: Speech - Speech recognition error
                                                             net.gotev.speech.SpeechRecognitionException: 3 - Audio recording error
                                                                 at net.gotev.speech.Speech$2.onError(Speech.java:180)
                                                                 at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java)
                                                                 at android.os.Handler.dispatchMessage(Handler.java)
                                                                 at android.os.Looper.loop(Looper.java)
                                                                 at android.app.ActivityThread.main(ActivityThread.java)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-06 23:24:06.822 9162-9162/skrip.si.findthissong I/speech: result: 
10-06 23:24:06.831 9162-9162/skrip.si.findthissong D/Speech: DelayedOperation - created delayed operation with tag: delayStopListening
10-06 23:24:06.862 9162-9215/skrip.si.findthissong D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=SpeechSearchDialog, firebase_previous_id(_pi)=-7656231050442859302, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-7656231050442859303}]


SpeechDialog Activity:

public class SpeechSearchDialog extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_speech_search_dialog);

        final SpeechProgressView progressView = findViewById(R.id.progress);
        final TextView resultText = findViewById(R.id.text_result);

        Logger.setLogLevel(Logger.LogLevel.DEBUG);

        try {
            // https://gotev.github.io/android-speech/
            Speech.init(this, getPackageName());
//            Speech.getInstance().setLocale(new Locale("id"));
            Speech.getInstance().setStopListeningAfterInactivity(6000);
            Speech.getInstance().startListening(progressView, new SpeechDelegate() {
                @Override
                public void onStartOfSpeech() {
                    Log.i("speech", "speech recognition is now active");
                }

                @Override
                public void onSpeechRmsChanged(float v) {
                    Log.d("speech", "rms is now: " + v);
                }

                @Override
                public void onSpeechPartialResults(List<String> results) {
                    StringBuilder str = new StringBuilder();
                    for (String res : results) {
                        str.append(res).append(" ");
                    }
                    Log.i("speech", "partial result: " + str.toString().trim());

                    resultText.setText(str.toString().trim());
                }

                @Override
                public void onSpeechResult(String result) {
                    Log.i("speech", "result: " + result);

                    Intent returnIntent = new Intent();
                    returnIntent.putExtra(StaticVariablesHelper.ARG_PATTERN, result);
                    setResult(Activity.RESULT_OK, returnIntent);

                    finish();
                }
            });
        } catch (SpeechRecognitionNotAvailable exc) {
            Log.e("Speech", "Speech recognition is not available on this device!");
        } catch (GoogleVoiceTypingDisabledException exc) {
            Log.e("Speech", "Google voice typing must be enabled!");
        } catch (Exception e) {
            Log.e("Speech", e+"");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Speech.getInstance().shutdown();
    }
}


I already granted the RECORD_AUDIO permission.

gotev commented 6 years ago

It's an error of the underlying Android SpeechRecognizer. So it's not a bug of the library itself.

https://developer.android.com/reference/android/speech/SpeechRecognizer.html#ERROR_AUDIO

On which Android version does this happen? It's a physical device or an emulator? Also, steps to reproduce?

uzuki-P commented 6 years ago

It's Android 6.0.1 on physical device. I just found the fix. It's look like the Google App also need the microphone permission granted.

Thanks for the fast response :smiley:

gotev commented 6 years ago

Actually, you don't have to request any permission, because it's all done through the Google App. Glad you've found the solution!

AjeenaSainDev commented 4 years ago

@uzuki-P How we enable same permission android STB, I am facing the same issue , please help me

gotev commented 4 years ago

That error depends on the Google App which is invoked by the library. Make sure you granted permissions to that before you use it.