8

In my app I am using TTS. I have 20 different activities which are changed when the user swipe left or right. According the activity, a text is spoken. I am executing tts with separate thread and activity selection is done with main thread. But the problem is very slow, the UI feels slugish. When I swipe left or right, once tts is finished speaking the text, the activity changes which shouldn't happen because I am using separate thread for tts. Here is the codE:

TTS class:

public class textToSpeech {

TextToSpeech tts=null;

public textToSpeech(Context con)
{
    tts = new TextToSpeech(con,new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {

            if(status != TextToSpeech.ERROR) // initialization me error to nae ha
            {
                tts.setPitch(1.1f); // saw from internet
                tts.setSpeechRate(0.4f); // f denotes float, it actually type casts 0.5 to float
                tts.setLanguage(Locale.US);
            }

        }
    });
}

public void SpeakText (String text)
{
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); // TextToSpeech.QUEUE_FLUSH forces the app to stop all the sounds that are currently playing before speaking this text
}


public void stopSpeak()
{
    tts.stop();
}

Gesture Reader Class: (separate class)

public void decideAlphabet()
{
    tts.stopSpeak();

    threadForTTS.start();

    switch (i)
    {
        case 0:
            activities=null;
            activities = new Intent(contxt,A.class); 
            contxt.startActivity(activities); 

            break;

        case 1:
            activities=null;
            activities = new Intent(contxt,B.class);
            contxt.startActivity(activities);

            break;
                   ....... 20 more case statements for selecting activities
              }

decideActivity() method is called when it is checked, which swipe was made, swipe to right or left.

NOTE:

Before adding tts in this app, the UI was performing properly without lag or slowness. After I added TTS, the app became slow. How can I solve this problem

Regards

user2498079
  • 2,872
  • 8
  • 32
  • 60

1 Answers1

8

I had the same problem and was about to comment on seeing the following logcat error ...skipped x many frames. The application may be doing too much work on its main thread.

Of course I was sure TTS was being called from another thread which I checked using Thread.currentThread().getName() But it turns out however that OnInit was indeed still running on the main thread and it looks like setting the language is an expensive operation. A quick change to run contents of onInit in a new thread and the UI freezing/choreographer complaining stopped:

@Override
public void onInit(int status) {
   new Thread(new Runnable() {
      public void run() {
         if(status != TextToSpeech.ERROR) // initialization me error to nae ha
         {
            tts.setPitch(1.1f); // saw from internet
            tts.setSpeechRate(0.4f); // f denotes float, it actually type casts 0.5 to float
            tts.setLanguage(Locale.US);
         }
      }
   }
}).start()
sham
  • 1,346
  • 1
  • 20
  • 28
  • Forgot to mention you might get a code error unless you set init status to final. – sham Jun 25 '14 at 00:22
  • Thank you very much! I have been straggling with this for a long tim. TTS initialization always kept frozen my UI screen. It has never crossed my mind that I could Override onInit method and run it in a Thread. I think this is only one functional solution for TTS being slow and freezint the main UI thread! – pajus_cz Nov 22 '14 at 14:30
  • 1
    I have tried this solution, but this won't work to any language outside English. It won't set the language correctly – mthandr May 10 '16 at 23:41
  • @mobilepotato7 How are you setting the language? Also, do you actually have those other TTS language packs installed on your device? – sham May 10 '16 at 23:55
  • @sham just setting the language using mTextToSpeech.setLanguage(locale); It works fine without putting it inside a background thread, but of course outside of it, it freezes the UI. – mthandr May 10 '16 at 23:57
  • spanish words will be read in english locale which is the default, for example. – mthandr May 10 '16 at 23:57
  • @mobilepotato7 So text is read out in spanish outside the thread? It's hard to say what the issue is without seeing some code. Are you sure you're not setting the language on a different instance of TTS perhaps? Btw, I haven't actually tried changing the language myself, I just moved OPs existing code into a new thread. – sham May 11 '16 at 00:07