我想听用户说的第一个字。语音识别开始时,我会听到两声,但是立即听到停止音。我观察到onRMSchanged是发生事情的唯一方法。
这就是我使用SpeechRecognizer的方式。运行应用程序时,我已连接到互联网。
Intent speechintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS , 1000);
speechintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(Inference.getmContext());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
Log.i("beg", "beg");
}
@Override
public void onRmsChanged(float rmsdB) {
Log.i("on rms changed", "rms changed");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.i("ANSWER","END");
}
@Override
public void onEndOfSpeech() {
Log.i("ANSWER","END");
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
Log.i("ANSWER",results.getString(RecognizerIntent.EXTRA_RESULTS));
ArrayList<String> result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.i("ANSWER",partialResults.getString(RecognizerIntent.EXTRA_RESULTS));
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
speechRecognizer.startListening(speechintent);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speechRecognizer.stopListening();
}
}, 1000);
我不知道为什么它不让我说话。
更新:
这是方法执行的顺序:
onRmsChanged
onReadyForSpeech
onError
onError我得到了错误代码2,表示网络错误,但是我已经将Google文本设置为语音,我在清单中设置了INTERNET
和RECORD_AUDIO
权限,并且已连接到Internet。我不明白怎么了?