java-UtteranceProgressListener不会调用函数
作者:互联网
我正在尝试制作语音驱动的应用程序,但是遇到了一个主要问题.
无论我将Speak方法放在哪里,我的UtteranceProgressListener类都不会调用任何给定的方法.
这是我的代码:
这是我的OnCreate方法:
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的实现
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 one rror(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");
}
}
}
现在,当我按下按钮时,触发的事件是:
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()方法.为什么会这样呢?
我也尝试使用过时的setOnUtteranceListner(),结果相同.它不会触发任何方法,Toasts也不会出现.
请告知解决方法或解决方法.
我尝试过的设备是:
> API 19 Micromax帆布硝基
> API 21三星S4
> API 23(Marshmellow)华硕Zenfone
解决方法:
我终于弄清楚了为什么回调不起作用.原来,他们正在工作并在单独的线程上调用.因此,要执行常规功能,请在“ Activity.this.RunOnUiThread”中调用函数,并将其放入回调函数中.
标签:text-to-speech,java,android 来源: https://codeday.me/bug/20191118/2029839.html