如何访问布局TTS OnUtteranceCompleted回调? [英] How to access layout in TTS OnUtteranceCompleted callback?
问题描述
我正在运行助记符或数学练习用于训练目的一个简单的Android程序。
我有一个新的练习按钮,改变问题和答案。我想实现一个文本到语音模式,其中软件读取问题和答案。他们发言后,就应该算一个新的问题,然后重新开始。
I am making a simple Android program that runs mnemonics or math exercises for training purposes. I have a "new exercise" button that changes question and answer. I want to implement a "text to speech" mode, where the software reads the question and the answer. After they are spoken, it should calculate a new question and start again.
我使用OnUtteranceCompleted,以确定是否演讲已经结束。我的问题是我无法从回调访问我的新运动按钮。
I am using OnUtteranceCompleted to determine if the speech has ended. My problem is that I can’t access my "new exercise" button from the callback.
我使用这样的:
private TextToSpeech.OnUtteranceCompletedListener onUtteranceCompleted = new TextToSpeech.OnUtteranceCompletedListener()
{
@Override
public void onUtteranceCompleted(String utteranceId)
{
if(0 == utteranceId.compareToIgnoreCase(END_OF_SPEECH))
{
Log.i("TTS","Completed");
if (TTSMode == TTS_MODE_ON) {
//Start new
Log.i("TTS","TTS mode is on: start new exercize");
NewExercize();
btnNewEx.performClick();
}
}
}
};
我得到了以下错误:
$ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程
I got the following error: $CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我能够产生新的问题/答案,但我不能使用该按钮与新的问题或答案,至极改变字段使用TTS引擎。
I am able to generate the new question/answer, but I can’t use the button to change the fields with the new question or answer, wich are used by the TTS engine.
任何想法?
推荐答案
您需要使用 runOnUiThread
:
context.runOnUiThread( new Runnable() {
public void run() {
btnNewEx.performClick();
}
});
(可以省略背景。如果这是一个活动或服务子类)
(you can omit "context." if "this" is an Activity or Service subclass)
这篇关于如何访问布局TTS OnUtteranceCompleted回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!