Android Speech Recognition API does not work in Android 7 Nougat

拜拜、爱过 提交于 2019-12-05 14:07:51

问题


I am using the android.speech.SpeechRecognizer API for speech.

I works great in Android 4-5,

In Android 6 it now has a bunch of bugs, like the chime that occurs when the mic turns on is detected as speech, so it exists (and loops indefinitely when it restarts because to speech was detected, we have a hack workaround for this that sets the volume to 0 before the chime is played...)

In Android 6 the speech also dies with no error or anything after 5 seconds. We have another hack workaround for this that detects no activity for 5 seconds and restarts it...

Now in Android 7, the speech recognition does not appear to work at all? I have not been able to debug why as of yet, but has anyone had issues getting the speech API to work in Android 7?

Also, if anyone knows why Android seems to be adding new bugs in the speech API each release and not fixing them, please reply as well. Is this something that should be supported in Android, or do they want you to use the Google intent instead?


回答1:


My code works fine on Nexus5x(Nougat) and Nexus9(Nougat)

try and show logcat.

SpeechRecognizer mGoogleSr;

void initGoogleSr(Context context) {
    mGoogleSr = SpeechRecognizer.createSpeechRecognizer(context);
    mGoogleSr.setRecognitionListener(new GoogleSrListener());
}

void startGoogleSr() {
    if (mGoogleSr != null) {
        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, getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
        mGoogleSr.startListening(intent);
    }
}
void cancelRecognizing() {
    if (mGoogleSr != null) {
        mGoogleSr.cancel();
    }
}

public class GoogleSrListener implements RecognitionListener {
    String lastPartialText;

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.v(TAG, ">>> onReadyForSpeech");
        showMessage("ready");
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.v(TAG, ">>> onBeginningOfSpeech");
        showMessage("recognizing");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onEndOfSpeech() {
        Log.v(TAG, ">>> onEndOfSpeech");
        showMessage("waiting result");
    }

    @Override
    public void onError(int error) {
        Log.v(TAG, ">>> onError : " + error);
        switch (error) {
            case SpeechRecognizer.ERROR_AUDIO:
                Log.e(TAG, "ERROR_AUDIO");
                break;
            case SpeechRecognizer.ERROR_CLIENT:
                Log.e(TAG, "ERROR_CLIENT");
                break;
            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                Log.e(TAG, "ERROR_INSUFFICIENT_PERMISSIONS");
                break;
            case SpeechRecognizer.ERROR_NETWORK:
                Log.e(TAG, "ERROR_NETWORK");
                break;
            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                Log.e(TAG, "ERROR_NETWORK_TIMEOUT");
                break;
            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                Log.e(TAG, "ERROR_RECOGNIZER_BUSY");
                break;
            case SpeechRecognizer.ERROR_SERVER:
                Log.e(TAG, "ERROR_SERVER");
                break;
            case SpeechRecognizer.ERROR_NO_MATCH:
                Log.v(TAG, "ERROR_NO_MATCH");
                break;
            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                Log.v(TAG, "ERROR_SPEECH_TIMEOUT");
                break;
            default:
                Log.v(TAG, "ERROR_UNKOWN");
        }
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.v(TAG, ">>> onPartialResults");
        List<String> resultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (resultList != null) {
            String text = resultList.get(0);
            if (text.equals(lastPartialText)) {
                return;
            }
            lastPartialText = text;
            Log.v(TAG, "partial : " + text);
        }
    }

    @Override
    public void onResults(Bundle results) {
        Log.v(TAG, ">>> onResults");
        List<String> resultList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (resultList != null) {
            String text = resultList.get(0);
            Log.v(TAG, "result : " + text);
            showMessage(text);
        }
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.v(TAG, ">>> onEvent type = " + eventType);
    }
}

permissions in manifest(maybe redundant):

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />


来源:https://stackoverflow.com/questions/42494141/android-speech-recognition-api-does-not-work-in-android-7-nougat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!