在我的应用程序中,我正在使用TTS。我有20个不同的活动,当用户向左或向右滑动时这些活动会发生变化。根据活动,会朗读出一段文字。我使用单独的线程执行tts,并且活动选择是在主线程中完成的。但问题是非常缓慢,UI感觉很卡顿。当我向左或向右滑动时,一旦tts朗读完毕,活动就会改变,这不应该发生,因为我正在为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
}
当检查到向右或向左滑动时,将调用decideActivity()方法。
注意:
在此应用程序中添加tts之前,UI正常运行,没有延迟或缓慢。添加TTS后,应用变慢了。我该如何解决这个问题?
敬礼