UtteranceProgressListener 不会调用函数 [英] UtteranceProgressListener won't call the functions
问题描述
我正在尝试制作一个语音驱动的应用程序,但是我遇到了一个大问题.
无论我将 Speak 方法放在哪里,我的 UtteranceProgressListener
类都不会调用任何给定的方法.
I am trying to make a speech powered app, however I have run into a major problem.
My UtteranceProgressListener
Class will not call any of the given methods regardless of where I place the Speak method.
这是我的代码:
这是我的 OnCreate 方法:
This is my OnCreate Method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
voiceBtn = (Button) findViewById(R.id.startListeningBtn);
voiceBtn.setEnabled(false);
textToSpeech = new TextToSpeech(mContext,new botListener());
}
<小时>
这是 OnInitListner 实现
This is the OnInitListner Imeplementation
public class botListener implements TextToSpeech.OnInitListener{
@Override
public void onInit(int i) {
if(i == TextToSpeech.SUCCESS)
{
int s = textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
Toast.makeText(getApplicationContext(),"Done Speaking",Toast.LENGTH_SHORT).show();
}
@Override
public void onDone(String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String s) {
Toast.makeText(getApplicationContext(),"Done Speaking",Toast.LENGTH_SHORT).show();
}
});
Log.d(TAG,String.valueOf(s));
int result = textToSpeech.setLanguage(Locale.ENGLISH);
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e(TAG,"Language not supported");
Intent installLanguage = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installLanguage);
}
Log.d(TAG,"Started Voice Speaker");
}
else{
Log.e(TAG,"initialization failed");
}
}
}
<小时>
现在,当我按下按钮时,触发的事件是:
Now, when I press the button, the event that fires is:
public void initVoiceRecog(View v){
//Toast.makeText(mContext,"Clicked",Toast.LENGTH_SHORT).show();
Speak("hello","1");
// does some other things here after that
}
private void Speak(String text,String identifierID){
if(Build.VERSION.SDK_INT>21) {
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,identifierID);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, identifierID);
}
else{
// ttsMap is a HashMap
ttsMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,identifierID);
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,ttsMap );
}
}
<小时>
我的问题是,在打招呼后,它不会触发 OnStart()
或 OnError()
或 OnDone()
方法.为什么会这样?
My Question is, after saying hello, it does not fire the OnStart()
or the OnError()
or the OnDone()
methods. Why is this happening?
我也尝试过使用已弃用的 setOnUtteranceListner()
,结果相同.它不会触发任何方法,也不会显示 Toast.
I tried with the deprecated setOnUtteranceListner()
as well, same result. It does not fire any of the methods, the Toasts don't show up.
请说明修复方法或解决方法.
Please tell a fix or a workaround for this.
我试过的设备是:
推荐答案
我终于找到了回调不起作用的原因.事实证明,他们正在工作并调用一个单独的线程.因此,要执行正常功能,请调用Activity.this.RunOnUiThread"中的函数并将其放入回调函数中.
I finally figured out why the callbacks were not working. Turns out, they were working and calling on a separate thread. So to execute the normal functionality, call the functions in the 'Activity.this.RunOnUiThread' and put this in the call back fuctions.
这篇关于UtteranceProgressListener 不会调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!