TextToSpeech 默認不支持中文,需要第三方應用,這里推薦 “訊飛語記” 安裝后,將應用的錄音權限設定為允許,然后在系統的TTS設定下選擇 “訊飛語記” ,重啟使用TTS的應用即可,
一、使用TextToSpeech完成語音播放
public class TTSTextToSpeech implements TextToSpeech.OnInitListener {
private final static String TAG = TTSTextToSpeech.class.getSimpleName();
private static TTSTextToSpeech TTS_TEXT_TO_SPEECH;
private final TextToSpeech mTTS;
TTSTextToSpeech() {
//Utils.getApp() 是全域的application的變數
mTTS = new TextToSpeech(Utils.getApp(), this);
}
public static synchronized TTSTextToSpeech getInstance() {
if (null == TTS_TEXT_TO_SPEECH) {
TTS_TEXT_TO_SPEECH = new TTSTextToSpeech();
}
return TTS_TEXT_TO_SPEECH;
}
/**
* 播放,佇列式,依次播放
*/
public boolean speak(String text) {
// 設定音調,值越大聲音越尖(女生),值越小則變成男聲,1.0是常規
// mTTS.setPitch(1.0f);
// // 設定語速
// mTTS.setSpeechRate(1.0f);
//播放語音
return TextToSpeech.SUCCESS ==
mTTS.speak(text,
TextToSpeech.QUEUE_ADD,
null,
UUID.randomUUID().toString());
}
/**
* 停止,并移除所有后續佇列
*/
public boolean stop() {
return TextToSpeech.SUCCESS == mTTS.stop();
}
@Override
public void onInit(int status) {
//todo 系統不支持時使用 訊飛語記
// 判斷是否轉化成功
if (status == TextToSpeech.SUCCESS) {
//默認設定語言為中文,原生的android貌似不支持中文,
int result = mTTS.isLanguageAvailable(Locale.CHINA);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
LogUtils.w(TAG, "不支持中文..." + String.valueOf(result));
mTTS.setLanguage(Locale.US);
} else {
LogUtils.w(TAG, "支持中文..." + String.valueOf(result));
mTTS.setLanguage(Locale.CHINA);
}
}
}
}
二、log列印類
/**
* 用途:log列印,debug時才會列印
* <p>
* 作者:mjSoftKing
* 時間:2021/02/02
*/
public class LogUtils {
private final static boolean DEBUG = BuildConfig.DEBUG;
public static void e(Object tag, String msg) {
e(tag.getClass().getSimpleName(), msg);
}
public static void e(Object tag, String msg, Throwable tr) {
e(tag.getClass().getSimpleName(), msg, tr);
}
public static void w(Object tag, String msg) {
w(tag.getClass().getSimpleName(), msg);
}
public static void w(Object tag, String msg, Throwable tr) {
w(tag.getClass().getSimpleName(), msg, tr);
}
public static void d(Object tag, String msg) {
d(tag.getClass().getSimpleName(), msg);
}
public static void d(Object tag, String msg, Throwable tr) {
d(tag.getClass().getSimpleName(), msg, tr);
}
public static void i(Object tag, String msg) {
i(tag.getClass().getSimpleName(), msg);
}
public static void i(Object tag, String msg, Throwable tr) {
i(tag.getClass().getSimpleName(), msg, tr);
}
public static void e(String tag, String msg) {
if (DEBUG) {
Log.e(tag, msg);
}
}
public static void e(String tag, String msg, Throwable tr) {
if (DEBUG) {
Log.e(tag, msg, tr);
}
}
public static void w(String tag, String msg) {
if (DEBUG) {
Log.w(tag, msg);
}
}
public static void w(String tag, String msg, Throwable tr) {
if (DEBUG) {
Log.w(tag, msg, tr);
}
}
public static void d(String tag, String msg) {
if (DEBUG) {
Log.d(tag, msg);
}
}
public static void d(String tag, String msg, Throwable tr) {
if (DEBUG) {
Log.d(tag, msg, tr);
}
}
public static void i(String tag, String msg) {
if (DEBUG) {
Log.i(tag, msg);
}
}
public static void i(String tag, String msg, Throwable tr) {
if (DEBUG) {
Log.i(tag, msg, tr);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/286624.html
標籤:其他
上一篇:ios隨記2
