TTS - Character By Character



构建TTS应用程序

应用程序正在从sqlite的滑动上显示单词并立即说

但是,如果用户迅速滑动两次或三次跳过单词,它仍然说旧(即第一个单词(

说话

public void speak(){
    text = txt_word.getText().toString();
    final char[] chars = text.toCharArray();
    Toast.makeText(this, String.valueOf(chars.length), Toast.LENGTH_SHORT).show();
    textToSpeech.setPitch((float) 0.80);
    final boolean[] stop = {false};
    final int delay = 700;
    final int[] i = {0};
    final Handler handler=  new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                textToSpeech.speak(String.valueOf(chars[i[0]]),TextToSpeech.QUEUE_FLUSH,null,null);
            } else {
                textToSpeech.speak(String.valueOf(chars[i[0]]), TextToSpeech.QUEUE_FLUSH, null);
            }
            i[0]++;
            if(i[0]>=chars.length){
                stop[0] =true;
            }
            //do your work here..
            if (!stop[0]) {
                handler.postDelayed(this, delay);
            }
        }
    }, delay);
    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
    //Toast.makeText(this,text , Toast.LENGTH_SHORT).show();
}

滑动调用

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (e1.getX() > e2.getX()) {
        Cursor c = db.random();
        c.moveToFirst();
        txt_word.setText(c.getString(c.getColumnIndex("word")));
        speak();
    }

一点点google

onCreate------
Handler handler = new Handler();

创建方法

private Runnable speakCharackters = new Runnable() {
    @Override
    public void run() {
        chars = txt_word.getText().toString().toCharArray();
        textToSpeech.speak(String.valueOf(chars[i]),TextToSpeech.QUEUE_FLUSH,null,null);
        txt_character.setText(String.valueOf(chars[i]));
        i++;
        if(i>=chars.length){
            i=0;
            handler.removeCallbacks(speakCharackters);
        }else{
            handler.postDelayed(speakCharackters,delay);
        }
    }
};

,然后再开始启动新单词

handler.removeCallbacks(speakCharackters);
handler.postDelayed(speakCharackters, delay);

相关内容

  • 没有找到相关文章

最新更新