[英]Method call works only with a button click
I have been trying to get Android TTS working (with access from a different activity class). 我一直在尝试使Android TTS正常运行(可以从其他活动类进行访问)。 With the solution that I found, it only seems to work with a button click. 使用我找到的解决方案,似乎只能通过单击按钮来使用。 If I try to directly call the talk.speak() method it doesn't work for some strange reason. 如果我尝试直接调用talk.speak()方法,则由于某种奇怪的原因而无法使用。 I am very curious to find out what could be the problem here. 我很想知道这里可能是什么问题。
1) Creating a text to speech Initializer as follows: 1)创建文本语音转换初始化器,如下所示:
public class TextToSpeechInitializer{
private Context context;
private static TextToSpeech talk;
private TextToSpeechIniListener callback;
private Locale locale;
public TextToSpeechInitializer(Context context , Locale locale , TextToSpeechIniListener l) {
this.context = context;
if(l != null) {
callback = l;
}
this.locale = locale;
initialize();
}
private void initialize() {
talk = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
talk.setLanguage(locale); //TODO: Check if locale is available before setting.
callback.onSucces(talk);
}else{
callback.onFailure(talk);
Log.e("TTS","TextToSpeechInitializeError");
}
}
});
}
}
2) The above initializer class calls this interface class to notify the activity below (TTS Success or Failure) : 2)上面的初始化器类调用此接口类以通知以下活动(TTS成功或失败):
public interface TextToSpeechIniListener {
public void onSucces(TextToSpeech tts);
public void onFailure(TextToSpeech tts);
}
3) Activity 3)活动
public class Demo7 extends AppCompatActivity implements TextToSpeechIniListener {
private Button b;
private TextToSpeechInitializer i;
private TextToSpeech talk;
private boolean flag = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo7);
i = new TextToSpeechInitializer(this, Locale.UK, this);
b = (Button) findViewById(R.id.b);
// This doesn't work
if (flag)
talk.speak("Hello, Testing", QUEUE_ADD, null);
// This works
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag) {
talk.addEarcon("Attention", getPackageName(), R.raw.androidcalm);
talk.playEarcon("Attention", QUEUE_ADD, null);
//talk.setLanguage(Locale.UK);
talk.speak("Hello, Testing", QUEUE_ADD, null);
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(talk != null){
talk.stop();
talk.shutdown();
}
}
@Override
public void onSucces(TextToSpeech tts) {
this.talk = tts;
flag = true;
}
@Override
public void onFailure(TextToSpeech tts) {
flag = false;
finish();
}
}
layout demo7.xml 布局demo7.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/b"
android:text="speak"/>
</LinearLayout>
It's because you set your flag
to true
when onSuccess
is called and since onSuccess
is a callback it's called after your activity's onCreate
. 这是因为你设置你的flag
,以true
时onSuccess
被调用,因为onSuccess
是它的活动的呼吁后回调onCreate
。 So when your activity is created the flag
is always false. 因此,在创建活动时, flag
始终为false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.