android 文本转语音
16lz
2021-12-04
参考: 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); }}
更多相关文章
- build WebRTC for android
- android build (可参考之建立android编译环境)
- Android自动化测试之MonkeyRunner之MonkeyRunner(九)
- Android(安卓)支持多屏幕机制
- [Android] 问题记录 - Android(安卓)支持的度量单位
- Android(安卓)NDK带来什么
- TextView实现电话、网址链接
- android studio 使用cmake支持C++出现无法sync cmake error的问
- Android(安卓)的大牛的博客 提供给大家参考