use*_*079 7 java android text-to-speech
在我的应用程序中,我使用的是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();
}
Run Code Online (Sandbox Code Playgroud)
手势阅读器类:(单独的类)
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
}
Run Code Online (Sandbox Code Playgroud)
在选中后,调用decisionActivity()方法,进行滑动,向右或向左滑动.
注意:
在此应用程序中添加tts之前,UI正常运行,没有滞后或缓慢.我添加TTS后,应用程序变得缓慢.我怎么解决这个问题
问候
我有同样的问题,并准备评论看到以下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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3275 次 |
| 最近记录: |