0

After testing multiple different approaches of speech to text, i finally come to conclusion that the issue is from my specific device the code works fine on one device Android 5 while i keep receiving error on the other one which run Android 6

The second device is Chinese device using a custom sdk i'm wondering if there is a specific service that is needed in Android it self in order to enable the Speech Recognizer considering that i did install google voice app and did enable it using adb and same issue keep showing up

is there a way or Debugging Strategy should i use to find the source of this issue.

public class STT extends AppCompatActivity implements RecognitionListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//android view initialization removed

    String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
    if(!Function.hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION_KEY);
    }


    progressBar.setVisibility(View.INVISIBLE);
    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
            "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

    /*
    Minimum time to listen in millis. Here 5 seconds
     */
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5000);
    recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);




    recordbtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View p1)
        {
            progressBar.setVisibility(View.VISIBLE);
            speech.startListening(recognizerIntent);
            recordbtn.setEnabled(false);

            /*To stop listening
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
                recordbtn.setEnabled(true);
             */
        }


    });



}

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

@Override
protected void onPause() {
    super.onPause();
    if (speech != null) {
        speech.destroy();
        Log.d("Log", "destroy");
    }

}

@Override
public void onBeginningOfSpeech() {
    Log.d("Log", "onBeginningOfSpeech");
    progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.d("Log", "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
    Log.d("Log", "onEndOfSpeech");
    progressBar.setVisibility(View.INVISIBLE);
    recordbtn.setEnabled(true);
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d("Log", "FAILED " + errorMessage);
    progressBar.setVisibility(View.INVISIBLE);
    returnedText.setText(errorMessage);
    recordbtn.setEnabled(true);
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.d("Log", "onEvent");
}

 @Override
public void onPartialResults(Bundle arg0) {
    Log.d("Log", "onPartialResults");

    ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    /* To get all close matchs
    for (String result : matches)
    {
        text += result + "\n";
    }
    */
    text = matches.get(0); //  Remove this line while uncommenting above    codes


    returnedText.setText(text);
}

@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.d("Log", "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
    Log.d("Log", "onResults");

}

@Override
public void onRmsChanged(float rmsdB) {
    Log.d("Log", "onRmsChanged: " + rmsdB);
    progressBar.setProgress((int) rmsdB);

}


public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}
}
class Function {

public static  boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}
}

1 Answer 1

0

Yes there is a dependency on the "Google" app (which has the ID com.google.android.googlequicksearchbox):
you could check if the device has it installed, and if not, install it from Google's Play Store.

...
is there a way or Debugging Strategy should i use to find the source of this issue.

Is there no error message in the LogCat output?

1
  • i did install google service and enable it from adb using: (adb shell==>su==>pm enable com.google.android.googlequicksearchbox Got Package com.google.android.googlequicksearchbox new state: enabled ) still same the issue, i got Error Code 3 (Recording error) i believe the mic is occupied by another package which i cannot identify
    – Ninja
    Commented Oct 6, 2018 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.