为什么它在开始收听之前没有睡3秒钟?

时间:2013-07-31 12:37:19

标签: android speech-recognition text-to-speech

我正在尝试:

  1. 执行TextToSpeech
  2. 当用户重复时,SpeechRecognizer开始侦听 TextToSpeech'd单词/短语
  3. 但我的问题是,例如,如果我通过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);
    }
    

2 个答案:

答案 0 :(得分:2)

你正在两个线程中进行两个过程。您正在创建Thread One并使其休眠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);
    }
};

答案 1 :(得分: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();

}