如何在edittext中附加实时语音到文本的文本?

时间:2016-01-25 03:29:00

标签: android speech-recognition speech-to-text

我有一个Edittext我正在使用speech to text而没有Activity。我想在Edittext中附加文字,而不是覆盖。

我试过

editText.append(result)

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

但是。当我使用时,结果会重叠。

例如,如果我说abc dragon,则结果为abc dragon abc dragon

希望你的帮助!

这是我的IntentRecognitionListner代码:

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);

    }

}

3 个答案:

答案 0 :(得分:1)

我好像您首先append()result,然后从EditText对象中读取它并在追加{{1}后再次设置文本已经附加的文本。

仅使用

result

editText.append(result) 

答案 1 :(得分:1)

使用TextView.append()方法。

参数将附加在可编辑的末尾。

来自官方链接:

  

便捷方法:将指定的文本附加到TextView上   显示缓冲区,如果不是,则将其升级为BufferType.EDITABLE   已编辑。

例如:

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

如果您想设置唯一的新值,请使用此

editText.setText(title);

答案 2 :(得分:0)

您将当前文字存储在onBeginningOfSpeech

public String currentText;

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

然后在onPartialResultsonResults

中更新附加识别结果
@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));
}