1

I have an ArrayList of an Object "Words" that has two Strings attributes, which are a word in one language and its translation in another language. The words are taken from txt files that have them separated by the symbol "=" and so there is a word and its translation in each line. Like this:

word1Language1=word1language2
word2Language1=word2Language2
word3Language1=word3Language2

So, I read those words with a method that successfully puts them in the previously mentioned ArrayList. But now I want a bot to pronounce those words when I press a button (and I'd like it to stop too when I press the button again). This is my code currently, "wordsList" is the name of the ArrayList. I don't know how to tell the bot to wait between words (so it pronounces them properly):

mTTS = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR){
                    mTTS.setLanguage(Locale.US);
                }
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                for(int i=0; i <= wordsList.size(); i++){
                    mTTS.setLanguage(Locale.US);
                    String toSpeak1 = wordsList.get(i).getLanguage1();
                    mTTS.speak(toSpeak1, TextToSpeech.QUEUE_FLUSH, null);
                    mTTS.setLanguage(Locale.ITALIAN);
                    String toSpeak2 = wordsList.get(i).getLanguage2();
                    mTTS.speak(toSpeak2, TextToSpeech.QUEUE_FLUSH, null);
                }
                mTTS.shutdown();
            }
        });

Thanks!

sven
  • 13
  • 4
  • tts speak deprecated: https://stackoverflow.com/questions/30706780/texttospeech-deprecated-speak-function-in-api-level-21 – larsaars Jun 11 '20 at 00:04
  • also, TTS for me is not limited at all, almost all languages work. You may have to check if you downloaded all tts packages on your testing device – larsaars Jun 11 '20 at 00:05
  • @Lurzapps Thanks, I applied what you posted and managed to use other languages by instanciating a new Locale for each language, but I'm still unable to make it speak all the words because it doesn't stop between words. – sven Jun 11 '20 at 01:14
  • I added an answer – larsaars Jun 11 '20 at 09:36
  • Wait I have to edit it again – larsaars Jun 11 '20 at 09:37
  • I did not post how to do it exactly, since I've got no time for that right now (sorry), but with some googling you should get it to work – larsaars Jun 11 '20 at 09:44

1 Answers1

0

What you have to do is waiting for the tts to stop speaking the other words each time you call tts.speak(...). That you can do with a listener or a callback, I don't know how it is called, but just google "tts on finish speaking listener". When it has finished speaking, you can enter your next word and change the locale accordingly. What you're doing right now is telling the tts to play all words in the two languages at the same time and since the for loop does not wait as well, the next words are also inputted without waiting for the other inputs to have stopped speaking.

larsaars
  • 2,065
  • 3
  • 21
  • 32
  • 1
    Thanks, it worked. For anyone reading that might find it useful: I had to use "mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener(){}" and I had to change "QUEUE_FLUSH" for "QUEUE_ADD". – sven Jun 11 '20 at 17:07