我尝试用语音来发短信。 this link没问题,但我不想调用意图并使用对话框。
我创建了一个语音识别器实例,它会监听意图。
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()