方法调用仅适用于按钮单击

时间:2018-06-10 19:01:40

标签: java android text-to-speech

我一直在努力让Android TTS正常工作(从不同的活动类访问)。通过我找到的解决方案,它似乎只能按下按钮。如果我尝试直接调用talk.speak()方法,它不会因某些奇怪的原因而起作用。我非常想知道这里可能出现什么问题。

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)上面的初始化类调用此接口类来通知下面的活动(TTS成功或失败):

public interface TextToSpeechIniListener {

public void onSucces(TextToSpeech tts);

public void onFailure(TextToSpeech tts);
}

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

<?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>

1 个答案:

答案 0 :(得分:0)

这是因为您在调用flag时将true设置为onSuccess,并且onSuccess是一个回调,它是在您的活动onCreate之后调用的。因此,当您创建活动时,flag始终为false。