0

I am creating an application in that i need continuous speech recognition. But onReadyForSpeech calling two times.

I am also attaching my code. Please help me to find out the problem. Thanks in advance.

private SpeechRecognizer mSpeechRecognizer = null;
public static VoiceRecognizeService sVoiceRecognizeService;
private ITelephony mListener;
private boolean isListening;
private Intent mSaverController;

public VoiceRecognizeService() {
    super();
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    sVoiceRecognizeService = this;
    startListening();
    return START_NOT_STICKY;
}

public void setTelephonyListener(ITelephony mListener) {
    this.mListener = mListener;
}

public static VoiceRecognizeService getInstance() {
    return sVoiceRecognizeService;
}


// starts the service
public void startListening() {
    if (!isListening) {
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(this);
        Intent mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_IN");
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
        mRecognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
        mRecognizerIntent.putExtra("android.speech.extra.PREFER_OFFLINE", true);
        mRecognizerIntent.putExtra("calling_package", this.getPackageName());
        mSpeechRecognizer.startListening(mRecognizerIntent);
        isListening = true;
    }
}

public void processVoiceCommands(final ArrayList<String> partialData) {

}

public void restartListeningService() {
    cancelSpeechRecognition();
    startListening();
}


public void cancelSpeechRecognition() {
    if (mSpeechRecognizer != null) {
        mSpeechRecognizer.stopListening();
        mSpeechRecognizer.cancel();
        mSpeechRecognizer.destroy();
        mSpeechRecognizer = null;
        isListening = false;
    }
}

@Override
public void onReadyForSpeech(Bundle bundle) {
    Log.e("VoiceError", "speechReady");
}

@Override
public void onBeginningOfSpeech() {
}

@Override
public void onRmsChanged(float scale) {
    if (mListener != null) {
        mListener.onRmsChanged(scale);
    }
}

@Override
public void onBufferReceived(byte[] bytes) {
}

@Override
public void onEndOfSpeech() {
}

@Override
public void onError(int i) {
    if (i == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
    } else {
        restartListeningService();
    }
}

@Override
public void onResults(Bundle bundle) {
    final ArrayList<String> data = bundle.getStringArrayList(
            SpeechRecognizer.RESULTS_RECOGNITION);
    if (data != null) {
        processVoiceCommands(data);

    }
    restartListeningService();
}

@Override
public void onPartialResults(Bundle bundle) {
    final ArrayList<String> data = bundle.getStringArrayList(
            SpeechRecognizer.RESULTS_RECOGNITION);
    Log.e("VoiceError", "partialResults " + data);
}

@Override
public void onEvent(int i, Bundle bundle) {
}

@Override
public void onDestroy() {
    if (mSpeechRecognizer != null) {
        mSpeechRecognizer.setRecognitionListener(null);
        cancelSpeechRecognition();
    }
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
  • Assuming you are using Google as your recognition service, it has been troublesome recently. Check out the linked issues and gist from this post http://stackoverflow.com/a/38150919/1256219 to see if the BugRecognitionListener resolves your problem for now. – brandall Sep 27 '16 at 15:26
  • @brandall - thanks for the link, i already checked out your gist & comment. can you please help in one more thing. Continuous speech recognition as service draining & heating battery badly, do you have any solution for this? – Gaurav Vachhani Sep 28 '16 at 06:53
  • Did it solve the issue for you? I can't replicate it, running the latest Google Now beta. You need to use Pocketsphinx. It takes a little trial and error, but the resource usage is much, much lower. – brandall Sep 28 '16 at 07:13
  • @brandall - nope problem is same. I tried Pocketsphinx but that is not working better. – Gaurav Vachhani Sep 28 '16 at 07:21

0 Answers0