8

在我的应用程序中,我使用的是 TTS。当用户向左或向右滑动时,我有 20 种不同的活动会发生变化。根据活动,说出文本。我正在使用单独的线程执行 tts,并且活动选择是通过主线程完成的。但是问题很慢,UI感觉呆滞。当我向左或向右滑动时,一旦 tts 说完文本,活动就会发生变化,这不应该发生,因为我正在为 tts 使用单独的线程。这是代码:

TTS类:

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

手势阅读器类:(单独类)

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
              }

当它被检查时调用decisionActivity() 方法,滑动是向右还是向左滑动。

笔记:

在此应用程序中添加 tts 之前,UI 运行正常,没有延迟或缓慢。添加 TTS 后,应用程序变慢了。我怎么解决这个问题

问候

4

1 回答 1

8

我遇到了同样的问题,正要评论看到以下 logcat 错误...skipped x many frames. The application may be doing too much work on its main thread.

当然,我确信 TTS 是从我检查过的另一个线程调用的,Thread.currentThread().getName() 但事实证明,它OnInit确实仍在主线程上运行,并且看起来设置语言是一项昂贵的操作。onInit在新线程中运行内容的快速更改和 UI 冻结/编舞抱怨停止:

@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()
于 2014-06-25T00:21:36.523 回答