voice icon indicating copy to clipboard operation
voice copied to clipboard

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

Open AkshayOptimumbrew opened this issue 1 year ago • 0 comments

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

AkshayOptimumbrew avatar Jun 21 '24 07:06 AkshayOptimumbrew