Text To Speech app UI is slow android

亡梦爱人 提交于 2019-12-30 06:26:27

问题


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


回答1:


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()


来源:https://stackoverflow.com/questions/17258363/text-to-speech-app-ui-is-slow-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!