最近公司需求 制作语音提醒功能,
确保设备上又合成语音的引擎 我用的是 谷歌文字转语音引擎-google_18489_20160603.apk
注意 有些设备可能已经默认设置完成,可以先使用demo测试一下,播报失败再进行设置
设置引擎的方法如下
1、安装 谷歌文字转语音引擎-google_18489_20160603.apk
2、对设备进行设置
1)、设备联网
2)、设置---->无障碍---->文字转语音(TTS)输出---->首选引擎(不是设置)---->选择Google文字转语音引擎---->返回
3)、选择引擎语言 --->使用系统语言。
4)、点击 播放 是否有语音播放:有语音播放----设置成功,退出设置。
有些设备的 无障碍 会隐藏到设置的其他地方,请自行寻找,不要问,因为我也不知道,
相关代码.
public class speckutils {private static TextToSpeech tts;private static HashMap<String, String> params;private static String mostRecentUtteranceID;public static void init(Context context){charmap.put("0", "零,");charmap.put("1", "一,");charmap.put("2", "二,");charmap.put("3", "三,");charmap.put("4", "四,");charmap.put("5", "五,");charmap.put("6", "六,");charmap.put("7", "七,");charmap.put("8", "八,");charmap.put("9", "九,");charmap.put("a", "A,");charmap.put("b", "B,");charmap.put("c", "C,");charmap.put("d", "D,");charmap.put("e", "E,");charmap.put("f", "F,");charmap.put("g", "G,");charmap.put("h", "H,");charmap.put("i", "I,");charmap.put("j", "J,");charmap.put("k", "K,");charmap.put("l", "L,");charmap.put("m", "M,");charmap.put("n", "N,");charmap.put("o", "O,");charmap.put("p", "P,");charmap.put("q", "Q,");charmap.put("r", "R,");charmap.put("s", "S,");charmap.put("t", "T,");charmap.put("u", "U,");charmap.put("v", "V,");charmap.put("w", "W,");charmap.put("x", "X,");charmap.put("y", "Y,");charmap.put("z", "Z,");// ------------使用Google 语音转换引擎----------------------//Initialize TTStts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {@Overridepublic void onInit(int status) {//TTS初始化成功if (status == TextToSpeech.SUCCESS) {if (tts.getEngines().size() <= 0) {Log.i("Log", "请安装语音引擎");return;}//默认语言为中文int result = tts.setLanguage(Locale.CHINESE);if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {Log.i("Log", "不支持中文");//转中文失败,转英语tts.setLanguage(Locale.US);}}}});//改变音调和速度来改变声音效果tts.setPitch(0.9f);tts.setSpeechRate(1.0f);//Set unique utterance ID for each utterancemostRecentUtteranceID = (new Random().nextInt() % 9999999) + "";//Set params//This method will work for more devices: API 19+params = new HashMap<>();params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);// ------------使用Google 语音转换引擎----------------------}/*** 将字符串中的数字转换为汉字** @param text 要转换的字符串* @return 转换后的字符串*/private static Map<String, String> charmap = new HashMap<>();public static String format(String text) {StringBuffer buffer = new StringBuffer();for (int i = 0; i < text.length(); i++) {if (charmap.get(text.charAt(i) + "") == null) {buffer.append(text.charAt(i) + "");} else {buffer.append(charmap.get(text.charAt(i) + ""));}}return buffer.toString();}/*** 语音合成** @param string*/public static void speckstr(String string) {if (tts != null && !tts.isSpeaking()) {int code = tts.speak(format(string), TextToSpeech.QUEUE_FLUSH, params);Log.i("Log", code + "==code");Log.i("Log", format(string) + "==format(string)");}}public static void stopspeck(){tts.stop();}
}
使用 首先初始化 speckutils.init(this);
其次 speckutils.speckstr("text");