其他分享
首页 > 其他分享> > Android语音转文本::实时更新文本

Android语音转文本::实时更新文本

作者:互联网

我想在我的应用程序中将语音转换为文本.
为此,我正在使用RecognitionListener接口
一切正常,但是即使在说话时也如何更新和显示文本(例如在Google Now中进行语音搜索)

我已经将RecognizerIntent.EXTRA_PARTIAL_RESULTS设置为true
并且还使用了RecognitionListener接口的onPartialResults(Bundle arg()方法)来设置文本
语音识别完成后立即显示整个文本
但是我希望在用户讲话时显示实时文本

我的活动

public class MainActivity extends Activity implements RecognitionListener
{
private TextView returnedText;
private ToggleButton toggleButton;
private ProgressBar progressBar;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    returnedText = (TextView) findViewById(R.id.textView1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
    Button recordbtn = (Button) findViewById(R.id.mainButton);


    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);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);

    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setIndeterminate(true);
                    speech.startListening(recognizerIntent);
                } else {
                    progressBar.setIndeterminate(false);
                    progressBar.setVisibility(View.INVISIBLE);
                    speech.stopListening();
                }
            }
        });


    recordbtn.setOnLongClickListener(new OnLongClickListener(){

            @Override
            public boolean onLongClick(View p1)
            {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
                return true;
            }


        });



}

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

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

}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
    progressBar.setIndeterminate(false);
    progressBar.setMax(10);
}

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

@Override
public void onEndOfSpeech() {
    Log.i(LOG_TAG, "onEndOfSpeech");
    progressBar.setIndeterminate(true);
    toggleButton.setChecked(false);
}

@Override
public void one rror(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d(LOG_TAG, "FAILED " + errorMessage);
    returnedText.setText(errorMessage);
    toggleButton.setChecked(false);
}

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

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

    ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";

    returnedText.setText(text);
}

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

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

}

@Override
public void onRmsChanged(float rmsdB) {
    Log.i(LOG_TAG, "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;
}

}

如何发生这种情况

解决方法:

您无法使用Google API实现实时识别.在最佳情况下,当您使用OK Google或例如在Whatsup中识别以逐个单词添加文字的方式在Whatsup中识别时,您可以获得与google相同的结果:

recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);

Speechrecognizer花时间处理所有信息并将其拆分为信息,以便能够在您的应用程序中使用它.您可以查看下一篇文章,看看它是否对您优化应用有帮助:Make SpeechRecognizer Faster

希望对您有所帮助!

标签:speech-to-text,speech-recognition,android,textview
来源: https://codeday.me/bug/20191118/2029382.html