2

我正在尝试做:

  1. 执行一个TextToSpeech
  2. 当用户重复 TextToSpeech 的单词/短语时,SpeechRecognizer 开始收听

但我的问题是,例如,如果我要通过 TextToSpeech 说“示例”,当 SpeechRecognizer 开始收听时,它还会从之前的“示例”中获取并添加到用户所说的示例中。所以最后,我最终得到了我不想要的“示例”。

代码:

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    item = (String) parent.getItemAtPosition(position); 
    tts.speak(item, TextToSpeech.QUEUE_FLUSH, null);
    Thread thread = new Thread() {
        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    sr.startListening(srIntent);
}
4

2 回答 2

2

您正在两个线程中执行两个进程。您正在创建线程一并使其休眠 3 秒并sr.startListening(srIntent);在单独的 UI 线程中启动 Intent。所以它立即启动 Intent。在一个线程中使用这两个进程,就像我在下面发布的一样

public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
// TODO Auto-generated method stub
item = (String) parent.getItemAtPosition(position); 
tts.speak(item, TextToSpeech.QUEUE_FLUSH, null);
Thread thread = new Thread() {
    public void run() {
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    mSpeech.sendEmptyMessage(0);
    }
};
thread.start();

}

创建一个 Inner Handler 类来执行 UI 操作

private Handler mSpeech=new Handler(){
    public void handleMessage(android.os.Message msg) {
         sr.startListening(srIntent);
    }
};
于 2013-07-31T12:59:34.007 回答
0

它必须在run()体内

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    item = (String) parent.getItemAtPosition(position); 
    tts.speak(item, TextToSpeech.QUEUE_FLUSH, null);
    Thread thread = new Thread() {
        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           sr.startListening(srIntent);
        }
    };
    thread.start();

}
于 2013-07-31T12:57:33.793 回答