问题
I want to add a pause button to my TTS-app. I am trying to do this with a while-loop, which should playSIlence. It isn't working, but I can't find my mistake.
my boolean:
boolean pausegedrückt;
for-loop:
for (int i = 1; i < anzahl+1; i++) {
while (pausegedrückt==true) {
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
}
String str = String.valueOf(i);
tts.speak(str, TextToSpeech.QUEUE_ADD, null);
tts.playSilence(3000, TextToSpeech.QUEUE_ADD, null);
}
my onCheckedChanged
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
l.setBackgroundColor(Color.BLUE);
pausegedrückt=false;
} else {
l.setBackgroundColor(Color.BLACK);
pausegedrückt=true;
}
回答1:
I would suggest adding an OnUtteranceCompletedListener or an UtteranceProgressListener as utterances can fail on some devices without it.
Also, you are adding 1 second of silence to the queue for each loop. If it loops 10,000 times, you're going to end up with a lot of silence..... You need the loop inside the utterance listener (or at least another boolean parameter), so it will only add a further 1 second of silence once the previous silence finishes.
That said, I don't think this is the right approach to pausing the speech.
来源:https://stackoverflow.com/questions/22635249/tts-play-silence-in-a-for-loop-doesnt-work