这是我的第一篇文章,我希望你能帮助我:)
我正在为 android 开发一个应用程序,但我不明白为什么我的代码不起作用。 此应用程序识别语音以执行一些命令。
public void OnButtonClick(View v)
{
t.speak("Say the comand", TextToSpeech.QUEUE_FLUSH, null);
startVoiceRecognitionActivity();//This is another function (Speech input)
}
但我听不到 TextToSpeech。
但如果我尝试这段代码,它会起作用:
public void OnButtonClick(View v)
{
t.speak(x, TextToSpeech.QUEUE_FLUSH, null);
while (t.isSpeaking()); //It wait the end of the speech
startVoiceRecognitionActivity();//This is another function (Speech input)
}
没关系,但如果我在语音输入后尝试此代码,它什么也没说:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
b.setText(matches.get(0));
Act(matches.get(0)); //Call the function to do a comand
}
super.onActivityResult(requestCode, resultCode, data);
}
public void Act(String cmd){
if(cmd.equalsIgnoreCase("time") || cmd.equalsIgnoreCase("what time is it")){
t.speak(x, TextToSpeech.QUEUE_FLUSH, null); //don't speach!
while (t.isSpeaking());
}
}
我需要创建一个单独的线程还是有更好的解决方案?
感谢大家:)
附注对不起我的英语
最佳答案
尝试在您的Act(String cmd)
方法中添加一个日志命令以查看TTS 识别的内容(也许Android 无法识别命令):
public void Act(String cmd){
Log.d("MyTTSApp", cmd);
if(cmd.equalsIgnoreCase("time") || cmd.equalsIgnoreCase("what time is it")){
t.speak(x, TextToSpeech.QUEUE_FLUSH, null); //don't speach!
while (t.isSpeaking());
}
}
关于Android 文字转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456877/