参考: https://blog.csdn.net/shendan00/article/details/43525437#commentBox
https://www.jianshu.com/p/0cf01f089660
https://developer.android.google.cn/reerence/android/speech/tts/TextToSpeech
public class MyTTS extends UtteranceProgressListener implements TextToSpeech.OnInitListener {
private static final String TAG = MyTTS.class.getSimpleName();
private static MyTTS mInstance;
public static MyTTS getInstance() {
if (mInstance == null) {
synchronized (MyTTS.class) {
if (mInstance == null) {
mInstance = new MyTTS();
}
}
}
return mInstance;
}
private MyTTS() {
super();
}
private TextToSpeech mTextToSpeech;
public void init(Context context) {
mTextToSpeech = new TextToSpeech(context, this);
mTextToSpeech.setOnUtteranceProgressListener(this);
}
public void speak(String text) {
if (mTextToSpeech != null)
mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);
}
public void release() {
if (mTextToSpeech != null) {
mTextToSpeech.shutdown();
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS && mTextToSpeech != null) {
int result = mTextToSpeech.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED
|| result == TextToSpeech.ERROR) {
Log.d(TAG, "onInit 数据丢失或语言不支持");
}
if (result == TextToSpeech.LANG_AVAILABLE) {
Log.d(TAG, "onInit 支持该语言");
}
Log.d(TAG, "onInit ok");
}
}
@Override
public void onStart(String utteranceId) {
Log.d(TAG, "onStart " + utteranceId);
}
@Override
public void onDone(String utteranceId) {
Log.d(TAG, "onDone " + utteranceId);
}
@Override
public void onError(String utteranceId) {
Log.d(TAG, "onError " + utteranceId);
}
}