Android Speech Recognition API does not work in Android 7 Nougat

后端 未结 1 1428
余生分开走
余生分开走 2021-02-19 22:34

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

1条回答
  •  囚心锁ツ
    2021-02-19 22:57

    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 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 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):

    
    
    
    
    
    

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