UtteranceProgressListener不同步

时间:2015-02-14 00:28:57

标签: android text-to-speech

概要

  • 数组中的话语列表
  • 遍历数组并为每个话语调用我的方法“sayThis”
  • 正在调用UtteranceProgressListener,并且TTS正常工作
  • 调用我的方法“sayThis”似乎在UtteranceProgressListener完成当前话语之前被调用

我认为流程应该如下:

1)如果voiceReady = true,请拨打方法“sayThis” 2)TTS启动话语和UtteranceProgressListener onStart调用&将voiceReady设置为false
3)如果voiceReady = false,则不应调用方法“sayThis” 4)UtteranceProgressListener onDone在话语完成时调用,voiceReady = true
5)调用方法“sayThis”可以再次调用,因为voiceReady = true
6)上面应该继续,直到话语阵列循环完成为止

    //Code snippet that loops through array and calls "sayThis" method
    //There are no errors
    while(arrayCounter < utterances.size()) {
        if (voice.voiceReady == true) {
            voice.sayThis(utterances.get(arrayCounter), arrayCounter);
            arrayCounter++;
        }
    }

    //Code snippet that processes speech
    //There are no errors
    public void sayThis(String whatToSay, int utteranceID){
    Log.d(LOG_SOURCE, "sayThis method called: Utterance ID=" + utteranceID);
    voiceParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceID));
    ttsVoice.speak(whatToSay, TextToSpeech.QUEUE_ADD, voiceParams);        
}

    //Code snippet from the UtteranceProgressListener
    //You can see from the logs the methods are being called
    @Override
    public void onStart(String utteranceID) {
        voiceReady = false;
        Log.d(LOG_SOURCE, "(ttsUtteranceListener) UtteranceProgressListener onStart " +
                "method called for utteranceID:" + utteranceID + ", voiceReady=false");
    }

    @Override
    public void onDone(String utteranceID) {
        voiceReady = true;
        Log.d(LOG_SOURCE, "(ttsUtteranceListener) UtteranceProgressListener onDone " +
                "method called for utteranceID:" + utteranceID + ", voiceReady=true");

- Logcat表示并不总是遵循这种模式,如下所示:

02-13 18:05:25.487: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=0
02-13 18:05:25.490: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:0, voiceReady=false
02-13 18:05:29.578: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:0, voiceReady=true
02-13 18:05:29.578: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=1
02-13 18:05:29.580: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:1, voiceReady=false
02-13 18:05:33.107: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=2
02-13 18:05:33.107: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:1, voiceReady=true
02-13 18:05:33.109: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:2, voiceReady=false
02-13 18:05:38.667: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=3
02-13 18:05:38.667: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:2, voiceReady=true
02-13 18:05:38.668: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=4
02-13 18:05:38.669: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=5
02-13 18:05:38.671: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:3, voiceReady=false
02-13 18:05:45.411: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:3, voiceReady=true
02-13 18:05:45.411: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:4, voiceReady=false
02-13 18:05:49.966: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:4, voiceReady=true
02-13 18:05:49.966: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:5, voiceReady=false
02-13 18:05:54.198: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:5, voiceReady=true
02-13 18:05:54.199: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=6
02-13 18:05:54.202: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:6, voiceReady=false
02-13 18:05:55.912: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:6, voiceReady=true
02-13 18:05:55.912: D/LFS_TheVoice(10645): sayThis method called: Utterance ID=7
02-13 18:05:55.914: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onStart method called for utteranceID:7, voiceReady=false
02-13 18:05:56.765: D/LFS_TheVoice(10645): (ttsUtteranceListener) UtteranceProgressListener onDone method called for utteranceID:7, voiceReady=true

有关如何同步流量的任何建议?
它几乎就像通过数组的迭代一样,调用“sayThis”方法比UtteranceProgressListener更快地跟上切换“voiceReady”布尔值的真/假值。

如果我无法准确检测每个话语何时完成,这将导致流量控制问题。

感谢
迈克尔

1 个答案:

答案 0 :(得分:0)

创建班级成员

List<String> mUtterances = new ArrayList<String>();
int mCurrentUtteranceIndex;

将您的话语保存在mUtterances中,然后拨打sayThis(mUtterances.get(0), 0)

然后onDone拨打下一个sayThis

 @Override
public void onDone(String utteranceID) {
    if (++mCurrentUtteranceIndex < mUtterances.size()) {
        sayThis(mUtterances.get(mCurrentUtteranceIndex), mCurrentUtteranceIndex);
    }
}