Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to increase the amount of time Android speech recognition listens for? #505

Open
AkshayOptimumbrew opened this issue Jun 21, 2024 · 0 comments

Comments

@AkshayOptimumbrew
Copy link

AkshayOptimumbrew commented Jun 21, 2024

I want to increase the amount of time Android speech recognition.
I tried these 3 tags but it not working.
TAG 1: EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
TAG 2: EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
TAG 3: EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS

`private void startSpeechRecognizer() {
if (AppUtils.isValidContext(activity) && isAdded()) {
try {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

            intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 10000);
            intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);

            startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER);
        } catch (Throwable th) {
            showSnackbar(getString(R.string.speech_not_supported));
            th.printStackTrace();
        }
    }
}

private void showSnackbar(String message) {
    try {
        if (AppUtils.isValidActivity(activity) && isAdded() && linearTapAndSay != null) {
            Snackbar.make(linearTapAndSay, message, Snackbar.LENGTH_LONG).show();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (AppUtils.isValidContext(activity) && isAdded()) {
        try {
            if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
                if (resultCode == activity.RESULT_OK && data != null) {
                    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if (results != null && results.size() > 0) {
                        String detectWord = results.get(0);
                        ObLogger.e(TAG, "onActivityResult: mAnswer: " + detectWord);
                        if (detectWord != null && !detectWord.isEmpty() && editTextUserAnswer != null && editTextUserAnswer.getText() != null) {
                            int cursorPosition = editTextUserAnswer.getSelectionStart();
                            String existingText = editTextUserAnswer.getText().toString();
                            String newText = existingText.substring(0, cursorPosition) + detectWord + existingText.substring(cursorPosition);
                            editTextUserAnswer.setText(newText);
                            editTextUserAnswer.setSelection(cursorPosition + detectWord.length());
                            editTextUserAnswer.requestFocus();
                            if (relativeUserAnswerLong != null) {
                                relativeUserAnswerLong.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_rounded_corner_selected_onbaording));
                            }
                        }
                    }
                }
            }
        } catch (Throwable th) {
            showSnackbar(getString(R.string.speech_not_supported));
            th.printStackTrace();
        }
    }
}`

Does anyone have solution then please help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant