我尝试将语音用于文本。这个例子没问题,但我不想调用意图并使用对话框。
我创建了一个语音识别器实例,它会监听意图。
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(getContext());
sr.setRecognitionListener(new RecognitionListener() {
@Override
public void onResults(Bundle b) {
Log.i("TAG", " onResults "); }
@Override
public void onBeginningOfSpeech() {
Log.i("TAG", " onBeginningOfSpeech ");
}@Override
public void onRmsChanged(float rmsdB) {
Log.i("TAG" , " onRmsChanged " + rmsdB);
}
@Override
....
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
sr.startListening(intent);
当我运行此代码时,onRmsChanged 会触发多次。所以我注释掉Log.i("TAG" , " onRmsChanged " + rmsdB);
。
onReadyForSpeech -> onBeginningOfSpeech -> onEndOfSpeech -> onResults。但是超时时间太短了。现在,我想静静地等一会儿。我能怎么做?它应该听,直到我打电话sr.stoplistening()