本文介绍了文本到语音应用程序的用户界面很慢的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序,我使用TTS。我有当向左或向右轻扫用户这是改变20个不同的活动。据活动,文本是口头的。我执行TTS有单独的线程和活动选择与主线程中完成的。但问题是很慢的,用户界面感觉slugish。当我向左或向右滑动,一旦TTS完成读出文本,这是不应该的,因为我使用单独的线程来TTS活动的变化。这里是code:
TTS类:的
公共类textToSpeech {
TextToSpeech TTS = NULL;
公共textToSpeech(上下文CON)
{
TTS =新TextToSpeech(CON,新TextToSpeech.OnInitListener(){
@覆盖
公共无效的OnInit(INT状态){
如果(状态!= TextToSpeech.ERROR)//初始化我错误NAE公顷
{
tts.setPitch(1.1F); //锯从互联网
tts.setSpeechRate(0.4f); // f表示浮点数,它实际上类型转换0.5浮
tts.setLanguage(Locale.US);
}
}
});
}
公共无效SpeakText(字符串文本)
{
tts.speak(文字,TextToSpeech.QUEUE_FLUSH,NULL); // TextToSpeech.QUEUE_FLUSH迫使应用程序来停止所有当前播放的声音讲这段文字之前,
}
公共无效stopSpeak()
{
tts.stop();
}
手势读取器类:(单独的类)的
公共无效decideAlphabet()
{
tts.stopSpeak();
threadForTTS.start();
开关(ⅰ)
{
情况下0:
活动= NULL;
活动=新的意图(contxt,的A.class);
contxt.startActivity(活动);
打破;
情况1:
活动= NULL;
活动=新的意图(contxt,B.class);
contxt.startActivity(活动);
打破;
选择活动....... 20多个case语句
}
当它被check
decideActivity()方法被调用,遂向其刷卡,刷卡向左或向右。
请注意:
在在此应用程序加入TTS,在UI进行了不正确的滞后或缓慢。当我加入TTS的应用程序变得缓慢。我怎样才能解决这个问题。
问候
解决方案 ...跳过x多帧。该应用程序可能会做它的主线程的工作太多了。
当然,我肯定TTS正在从我检查使用 Thread.currentThread()另一个线程调用。的getName()
但事实却是的OnInit
确实仍是主要的线程上运行,它看起来像设置语言是一种昂贵的操作。快速变化的一个新的线程和UI冻结/编舞执行的OnInit
的内容,停止抱怨:
@覆盖
公共无效的OnInit(INT状态){
新主题(新的Runnable(){
公共无效的run(){
如果(状态!= TextToSpeech.ERROR)//初始化我错误NAE公顷
{
tts.setPitch(1.1F); //锯从互联网
tts.setSpeechRate(0.4f); // f表示浮点数,它实际上类型转换0.5浮
tts.setLanguage(Locale.US);
}
}
}
})。开始()
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
解决方案
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()
这篇关于文本到语音应用程序的用户界面很慢的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!