TextToSpeech OnUtteranceCompletedListener导致延迟

时间:2014-03-18 15:31:45

标签: android text-to-speech onutterancecompleted

我需要TextToSpeech引擎逐个说出我的话,我试图抓住一个单词的结尾,开始讲下一个单词。但OnUtteranceCompletedListener引起了一些延迟。 所以我的问题是,如何解决这个问题或者更好地实现OnUtteranceCompletedListener

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {

    int result = 0, CURRENT_WORD = 0;
    HashMap<String, String> myHash;
    String[] words;
    Button btnSpeak;
    TextToSpeech tts;
    Handler hand = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = "Hi there how are you";
        words = text.split(" ", 50);
        myHash = new HashMap<String, String>();
        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                hand.postDelayed(run, 300);
            }
        });
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            result = tts.setLanguage(Locale.getDefault());
            tts.setPitch(1f);
            tts.setSpeechRate(1f);
        } else
            Log.e("TTS", "Init failed");
    }

    Runnable run = new Runnable() {
        public void run() {
            text = words[CURRENT_WORD];
            tts.speak(text, 1, myHash);
            tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(String utteranceId) {
                    CURRENT_WORD++;
                    hand.post(run1);
                }
            }); 
        }
    };

}

2 个答案:

答案 0 :(得分:2)

您可以通过不在每次运行中重新创建OnUtteranceCompleteListener来加快速度

OnUtteranceCompletedListener listener=new OnUtteranceCompletedListener(){
    @Override
    public void onUtteranceCompleted(String utteranceId) {
        CURRENT_WORD++;
        hand.post(run1);
    }
}
tts.setOnUtteranceCompletedListener(listener);
Runnable run = new Runnable() {
    public void run() {
        text = words[CURRENT_WORD];
        tts.speak(text, 1, myHash);
    }
};

更确切地说,您可以使用onUtteranceCompleted方法调用speak()方法,而不是使用Runnable通过处理程序调用引擎的speek()方法

OnUtteranceCompletedListener listener=new OnUtteranceCompletedListener(){
    @Override
    public void onUtteranceCompleted(String utteranceId) {
        CURRENT_WORD++;
        if(CURRENT_WORD<max_words){
            String text=words[CURRENT_WORD];
            tts.speak(text,1,myHash);
        }
    }
}
tts.setOnUtteranceCompletedListener(listener);
Runnable run = new Runnable() {
    public void run() {
        text = words[CURRENT_WORD];
        tts.speak(text, 1, myHash);
    }
};

答案 1 :(得分:0)

如果不支持Locale.getDefault()语言或需要数据文件,您的代码将失败。此外,如果在您按下onInit()btnSpeak未返回300毫秒,则speak()将无法运行。 您应该在xml布局文件中禁用btnSpeak并在onInit中启用它。在btnSpeak listener循环播放words并致电speak()

btnSpeak = (Button) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            for (int i = 0; i < words.length; i++) {
                tts.speak(text, 1, myHash);
                // call playSilence (long durationInMs, 1, myHash) 
                // if you want a slight delay between each word.
            }
        }
    });