Android 文字转语音

标签 android

这是我的第一篇文章,我希望你能帮助我:)

我正在为 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/

相关文章:

android - BroadcastReceiver 应该在 Activity 中声明吗?

安卓 OGLES 2 : shaders pointer for color

android - 使用 Retrofit 和 Realm 在 RxJava 中纠正流程

java - tearDown() 应该在每次测试后运行吗?

android drawable 无效的开始标签

android - 在 Android (Samsung Tab 2) 浏览器上替代 ☐ 和 ☒

android - 在android应用程序上写问题

android - 如何在 android 中单击图像按钮时从视频 url 播放视频?

java - 正则表达式 - 不要选择双引号内的评论

android - 在 AppBarLayout 上投影