在我的应用中,我正在使用TTS。我有20个不同的活动,当用户向左或向右滑动时会更改这些活动。根据活动,会说一个文字。我正在使用单独的线程执行tts,并且主线程完成了活动选择。但问题非常缓慢,用户界面感觉很笨拙。当我向左或向右滑动时,一旦tts完成说出文本,活动就会发生变化,因为我正在使用单独的线程进行tts。 这是codE:
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后,应用程序变得缓慢。我该如何解决这个问题
此致
答案 0 :(得分:7)
我遇到了同样的问题,并且即将评论看到以下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()