Java Android静态TextToSpeech

Java Android静态TextToSpeech,java,android,static,text-to-speech,Java,Android,Static,Text To Speech,我的问题是,我有一个Android应用程序,它应该告诉用户一些事情。我想要点像这样的 Speaker.say("Hello World!"); // Wait till sentence is said Speaker.say("Its " + time); // Wait again NextCommand.xyz(); 我试过了 import android.speech.tts.TextToSpeech; public class TTS implements TextToSpeech

我的问题是,我有一个Android应用程序,它应该告诉用户一些事情。我想要点像这样的

Speaker.say("Hello World!");
// Wait till sentence is said
Speaker.say("Its " + time);
// Wait again
NextCommand.xyz();
我试过了

import android.speech.tts.TextToSpeech;

public class TTS implements TextToSpeech.OnInitListener {
private static TextToSpeech mTts;
private String text;
private static final TTS helper = new TTS();

public static TTS getInstance(){

    return helper;
}


public void say(String text){
    if(mTts == null){
        this.text = text;
        mTts = new TextToSpeech(context /* ignore this please */, helper);
        mTts.setPitch(3);

    }
    else{
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}


@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

public void stopTTS(){
    if(mTts != null){
        mTts.shutdown();
        mTts.stop();
        mTts = null;
    }
}

}
然后在onCreate中:

TTS tts = TTS.getInstance();
tts.say("Hello World");
tts.say("Hello again!")
但是: 只说第一句话,然后创建
选项菜单

怎么做?

我解决了这个问题:

import android.speech.tts.TextToSpeech;

import java.util.Locale;

public class Test implements TextToSpeech.OnInitListener {
    TextToSpeech tts;
    String text;

    public Test(String text) {
        tts = new TextToSpeech(MyApplication.getContext(), this);
        this.text = text;
    }

    @Override
    public void onInit(int i) {
        if (i == TextToSpeech.SUCCESS) {
            tts.setLanguage(Locale.GERMAN);
            tts.setPitch(3);
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            while(tts.isSpeaking());
        }
    }
}
然后你称之为:

new Test("Hello");
new Test("Hello again!");
Toast.makeText(this, "Finished", Toast.LENGTH_LONG).show();