TaeWoo Park
TaeWoo Park

Reputation: 1

How to append text in edittext with real time speech to text?

I have an Edittext for which I am using speech to text without Activity. I would like to append text in the Edittext instead of overwriting.

I tried

editText.append(result)

editText.getText()
editText.setText(gotText + result)

but. when i use, result be overlapping.

For example , if i speak abc dragon, result is abc dragon abc dragon

Hope your help !

This is my Intent and RecognitionListner code:

public void recognizeDirectly(Intent recognizerIntent)  {
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

    SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
    recognizer.setRecognitionListener(listener);
    recognizer.startListening(recognizerIntent);
}



private RecognitionListener listener = new RecognitionListener() {

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d(TAG, "Ready for speech " + params);
    }
    @Override
    public void onBeginningOfSpeech() {
        progressBar1.setMax(10);
    }
    @Override
    public void onRmsChanged(float rmsdB) {
        progressBar1.setProgress((int) rmsdB);
    }
    @Override
    public void onBufferReceived(byte[] buffer) {
    }
    @Override
    public void onEndOfSpeech() {
    }
    @Override
    public void onError(int error) {
        Log.d(TAG, getErrorText(error));
    }

    @Override
    public void onResults(Bundle results) {
        Log.d(TAG, "full results");
    }
    @Override
    public void onPartialResults(Bundle partialResults) {
            receiveResults(partialResults);
    }
    @Override
    public void onEvent(int eventType, Bundle params) {

    }
};

private void receiveResults(Bundle results)  {

    if ((results != null) && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))  {
        List<String> heard = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        resultText = heard.get(0);
        txtText.append(resultText);

    }

}

Upvotes: 0

Views: 2613

Answers (3)

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25220

You store current text in onBeginningOfSpeech:

public String currentText;

@Override
public void onBeginningOfSpeech() {
    currentText = txtText.getText();
}

and then update append recognition result in onPartialResults and in onResults

@Override
public void onResults(Bundle results) {
    txtText.setText(currentText + " " +
                    results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
    txtText.setText(currentText + " " +
                    results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0));
}

Upvotes: 0

Amit Vaghela
Amit Vaghela

Reputation: 22965

Use the TextView.append() method.

The Argument will be appended at the end of the Editable.

From Official Link :

Convenience method: Append the specified text to the TextView's display buffer, upgrading it to BufferType.EDITABLE if it was not already editable.

For Example :

String title = bundle.getString("number1");
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append(title);

If you want to set the only new value use this

editText.setText(title);

Upvotes: 1

Onur
Onur

Reputation: 347

I seems like you're first append()'ing the result, and then reading it from the EditText object and setting the text again after you append the result to the already appended text.

Use only either

editText.append(result) 

or

String gotText = editText.getText()
editText.setText(gotText + result)

Upvotes: 1

Related Questions