构建TTS应用程序
App正在从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();
}
SWIPE CALL
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();
}