Skip to content

Instantly share code, notes, and snippets.

@brandall76
Last active March 29, 2018 04:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save brandall76/bbf908d886c9a3469e6957812555f1f7 to your computer and use it in GitHub Desktop.
Save brandall76/bbf908d886c9a3469e6957812555f1f7 to your computer and use it in GitHub Desktop.
Workaround to current issues with the Google Recognition Service.
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.util.Log;
/**
* Class to handle the current bugs in the Google recognition service.
* <a href="https://gist.github.com/brandall76/b911eb732f309dec68af351d74013851">Speech Test/a>
* <p>
* The boolean parameters could of course just be plonked in any implementation of
* {@link RecognitionListener} but subclassing them like this keeps them out of view...
* <p>
* Call {@link BugRecognitionListener#resetBugVariables()} each time before starting the recognition.
* <p>
* Created by benrandall76@gmail.com on 22/06/2016.
*/
public class BugRecognitionListener implements RecognitionListener {
private final String CLS_NAME = BugRecognitionListener.class.getSimpleName();
private boolean doError;
private boolean doEndOfSpeech;
private boolean doBeginningOfSpeech;
public void resetBugVariables() {
Log.i(CLS_NAME, "resetBugVariables");
doError = false;
doEndOfSpeech = false;
doBeginningOfSpeech = false;
}
/**
* Called when the endpointer is ready for the user to start speaking.
*
* @param params parameters set by the recognition service. Reserved for future use.
*/
@Override
public void onReadyForSpeech(final Bundle params) {
doError = true;
doEndOfSpeech = true;
doBeginningOfSpeech = true;
}
@Override
public void onBeginningOfSpeech() {
Log.i(CLS_NAME, "onBeginningOfSpeech: doEndOfSpeech: " + doEndOfSpeech);
Log.i(CLS_NAME, "onBeginningOfSpeech: doError: " + doError);
Log.i(CLS_NAME, "onBeginningOfSpeech: doBeginningOfSpeech: " + doBeginningOfSpeech);
if (doBeginningOfSpeech) {
doBeginningOfSpeech = false;
onBeginningOfRecognition();
}
}
public void onBeginningOfRecognition() {
}
@Override
public void onRmsChanged(final float rmsdB) {
}
@Override
public void onBufferReceived(final byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
Log.i(CLS_NAME, "onEndOfSpeech: doEndOfSpeech: " + doEndOfSpeech);
Log.i(CLS_NAME, "onEndOfSpeech: doError: " + doError);
Log.i(CLS_NAME, "onEndOfSpeech: doBeginningOfSpeech: " + doBeginningOfSpeech);
if (doEndOfSpeech) {
onEndOfRecognition();
}
}
public void onEndOfRecognition() {
}
@Override
public void onError(final int error) {
Log.w(CLS_NAME, "onError: doEndOfSpeech: " + doEndOfSpeech);
Log.w(CLS_NAME, "onError: doError: " + doError);
Log.w(CLS_NAME, "onError: doBeginningOfSpeech: " + doBeginningOfSpeech);
if (doError) {
onRecognitionError(error);
}
}
public void onRecognitionError(final int error) {
}
@Override
public void onResults(final Bundle results) {
}
@Override
public void onPartialResults(final Bundle partialResults) {
}
@Override
public void onEvent(final int eventType, final Bundle params) {
}
// USAGE
private final BugRecognitionListener recognitionListener = new BugRecognitionListener() {
/**
* MUST CALL SUPER!
*/
@Override
public void onReadyForSpeech(final Bundle params) {
super.onReadyForSpeech(params);
}
/**
* Instead of {@link RecognitionListener#onEndOfSpeech()}
*/
@Override
public void onEndOfRecognition() {
}
/**
* Instead of {@link RecognitionListener#onError(int)}
*
* @param error the error code
*/
@Override
public void onRecognitionError(final int error) {
}
/**
* Instead of {@link RecognitionListener#onBeginningOfSpeech()}
*/
@Override
public void onBeginningOfRecognition() {
}
@Override
public void onBufferReceived(final byte[] buffer) {
}
@Override
public void onEvent(final int eventType, final Bundle params) {
}
@Override
public void onPartialResults(final Bundle partialResults) {
}
@Override
public void onResults(final Bundle results) {
}
@Override
public void onRmsChanged(final float rmsdB) {
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment