我需要 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);
}
});
}
};
}