android中的文本到语音异常行为
我有一种从文本到语音的奇怪体验 请参阅我的代码:android中的文本到语音异常行为,android,text-to-speech,Android,Text To Speech,我有一种从文本到语音的奇怪体验 请参阅我的代码: Button b; TextView title, body; TextToSpeech tts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popups); b = (Button) findViewById(R.id.b
Button b;
TextView title, body;
TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popups);
b = (Button) findViewById(R.id.buttonok);
title = (TextView) findViewById(R.id.textViewtitle);
body = (TextView) findViewById(R.id.textViewbody);
b.setText("ok");
title.setText(Receive.address);
body.setText(Receive.body);
tts = new TextToSpeech(Popup.this, new TextToSpeech.OnInitListener() {
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
play();//this is not working??!!i don't know why
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});
}
private void play() {
// TODO Auto-generated method stub
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
我的文本到语音转换只有在单击按钮时才能正常工作,但每当我不单击此按钮并在普通代码中编写tts.speak()时,它就不工作了……为什么
关于charlie在开始讲话之前,您必须等待呼叫onInit。在代码中,在声明之后立即调用play()。onInit是一个回调,在调用它之前需要一些时间。如果您在按钮出现时立即单击按钮,有时它会失败,因为尚未调用onInit。您应该有一个类成员
boolean mIsReady
,并在onInit中将其设置为true。在您的play()
方法中
private void play() {
// TODO Auto-generated method stub
if (mIsReady) {
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
}
在设置其setOnClickListener
之前,您正在使用b.performClick()
执行单击。另外,最好使用onResume()
方法进行此类调用。OnCreate用于绑定视图和准备活动。在前台向用户显示视图之前,将调用onResume()
方法,以便在前台放置此代码
看看活动生命周期
谢谢!我已经被困在这个问题上好几天了!
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});