在我的应用程序中,我正在使用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
}当检查decideActivity()方法时,将调用该方法,该方法是进行滑动,向右或向左滑动。
注意:
在此应用程序中添加tts之前,UI运行正常,没有滞后或缓慢。在我添加了TTS之后,应用程序变得很慢。我如何解决这个问题?
问候
发布于 2014-06-25 00:21:36
我也遇到了同样的问题,在看到下面的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()https://stackoverflow.com/questions/17258363
复制相似问题