android 文本转语音
16lz
2021-01-23
参考: 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); }}
更多相关文章
- C语言函数以及函数的使用
- Android 多国语言文件夹
- Android的init过程(二):初始化语言(init.rc)解析
- Android重启应用程序 && 不重启应用不改变系统语言改变 Android
- Android Interface Definition Language (AIDL) android接口定义
- android 多语言实现总结
- android源码分析(一) - 语言切换机制
- Android接口定义语言(AIDL)