我希望在使用TextToSpeech提出问题后,能够听取用户的回答。但是,即使我在清单中设置了INTERNET
和RECORD_AUDIO
权限,也会收到Error_NETWORK。我将Google文字转语音设置为引擎,并已连接到网络。
这是我使用语音识别器监听器的方式。
// Intent to listen to user vocal input and return the result to the same activity.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Use a language model based on free-form speech recognition.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS , 10000);
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(Inference.getmContext());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
Log.i("SPEECH", "On ready for speech");
}
@Override
public void onBeginningOfSpeech() {
Log.i("SPEECH", "On beggining of speech");
}
@Override
public void onRmsChanged(float rmsdB) {
Log.i("SPEECH", "On rms changed");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.i("SPEECH", "On buffer received");
}
@Override
public void onEndOfSpeech() {
Log.i("SPEECH", "On end of speech");
}
@Override
public void onError(int error) {
Log.i("SPEECH", "On error " + error);
}
@Override
public void onResults(Bundle results) {
Log.i("ANSWER",results.getString(RecognizerIntent.EXTRA_RESULTS));
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.i("ANSWER",partialResults.getString(RecognizerIntent.EXTRA_RESULTS));
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
speechRecognizer.startListening(intent);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speechRecognizer.stopListening();
}
}, 10000);
我只听到语音的开始,然后立即又听到停止的声音。 “文本转语音”有效,但“语音转文本”无效。
这是我使用TextToSpeech的方式,直到它说完为止,然后启动SpeechRecognizer。一切都发生在非活动类中,但是使用活动类中的上下文。
textToSpeech= MenuActivity.getText();
int speechStatus= textToSpeech.speak("You seem nervous this morning. Are you in a hurry?",TextToSpeech.QUEUE_FLUSH,null,null);
if (speechStatus == TextToSpeech.ERROR) {
Log.i("TTS", "Error in converting Text to Speech!");
}
while(textToSpeech.isSpeaking()) {
Log.i("WAIT", "waiting....");
}
我不知道如何解决这个问题。