12

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?

James
  • 17,965
  • 11
  • 91
  • 146
  • 1
    what device are u using? many nugat devices do not support some 64bit apps even if they say they do. try building it with 32bit. i had a similar problem and solved it by this way. – Andreas Constantinou Feb 27 '17 at 20:07
  • 1
    The API has not changed for ~2 years (see https://android.googlesource.com/platform/frameworks/base/+log/refs/heads/master/core/java/android/speech). You are describing a problem in an implementation of this API, but you fail to mention which implementation is it (name, version number, etc.) Android itself does not contain a speech recognizer implementation, though many phones do have Google's implementation pre-installed. – Kaarel Feb 28 '17 at 22:39
  • 1
    Samsung G7 with Android 7, speech is not working – James Mar 01 '17 at 00:41
  • 1
    The other errors occurs on Samsung G6 with Android 6, and various other phone models we have tested, normally the older phones work better, newer ones, not so much – James Mar 01 '17 at 00:42
  • 1
    Are you sure you have a provider installed? `SpeechRecognizer.isRecognitionAvailable(mContext)`. Have you made sure the provider is not set to Vlingo in Samsung devices? It will tell you in code it works, but it doesn't. Google's implementation gets worse with every release. I would suggest switching to their Cloud Speech API if you want to handle everything yourself. – brandall Mar 01 '17 at 17:54
  • What do you mean provider installed? Samsung G7 should support speech out of the box like G6 G5... should it not? – James Mar 03 '17 at 13:16
  • Not if it's defaulted to Vlingo in the Voice Search Settings – brandall Mar 03 '17 at 22:43
  • Missing proper permission requests? – A P Mar 13 '17 at 13:18
  • The issue seem to be an issue with the RecognizerIntent properties being used. – James Jun 05 '17 at 20:38

1 Answers1

4

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" />
Joe Mizuno
  • 437
  • 2
  • 15