|
|
@@ -0,0 +1,173 @@
|
|
|
+package com.sheep.gamegroup.util;
|
|
|
+
|
|
|
+import android.speech.tts.TextToSpeech;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import com.bumptech.glide.Glide;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
+
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2018/12/25.
|
|
|
+ * realicing@sina.com
|
|
|
+ * 语音工具类
|
|
|
+ */
|
|
|
+public class TextToSpeechUtil {
|
|
|
+
|
|
|
+ private static TextToSpeechUtil instance;
|
|
|
+ /**
|
|
|
+ * Get the singleton.
|
|
|
+ *
|
|
|
+ * @return the singleton
|
|
|
+ */
|
|
|
+ public static TextToSpeechUtil get() {
|
|
|
+ if (instance == null) {
|
|
|
+ synchronized (Glide.class) {
|
|
|
+ if (instance == null) {
|
|
|
+ instance = new TextToSpeechUtil();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+ private TextToSpeech textToSpeech;
|
|
|
+ private TextToSpeech.OnInitListener onInitListener = new TextToSpeech.OnInitListener() {
|
|
|
+ /**
|
|
|
+ * 用来初始化TextToSpeech引擎
|
|
|
+ * status:SUCCESS或ERROR这2个值
|
|
|
+ * setLanguage设置语言,帮助文档里面写了有22种
|
|
|
+ * TextToSpeech.LANG_MISSING_DATA:表示语言的数据丢失。
|
|
|
+ * TextToSpeech.LANG_NOT_SUPPORTED:不支持
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onInit(int status) {
|
|
|
+ switch (status) {
|
|
|
+ case TextToSpeech.SUCCESS:
|
|
|
+ int result = textToSpeech.setLanguage(Locale.CHINA);
|
|
|
+ switch (result) {
|
|
|
+ case TextToSpeech.LANG_MISSING_DATA:
|
|
|
+ if (TestUtil.isTest())
|
|
|
+ G.showToast("语言的数据丢失");
|
|
|
+ break;
|
|
|
+ case TextToSpeech.LANG_NOT_SUPPORTED:
|
|
|
+ if (TestUtil.isTest())
|
|
|
+ G.showToast("不支持朗读【中文】文本信息");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ if(!TextUtils.isEmpty(msg)){//文本不为空,朗读文本
|
|
|
+ if(pitch != -1){
|
|
|
+ speakMsg(msg, pitch);
|
|
|
+ pitch = -1;
|
|
|
+ } else {
|
|
|
+ speakMsg(msg);
|
|
|
+ }
|
|
|
+ msg = null;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ if (TestUtil.isTest())
|
|
|
+ G.showToast("不支持朗读文本信息");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ private float pitch = -1;
|
|
|
+ private String msg;
|
|
|
+
|
|
|
+ //初始化
|
|
|
+ private void initTextToSpeech() {
|
|
|
+ if(textToSpeech == null){
|
|
|
+ try {
|
|
|
+ textToSpeech = new TextToSpeech(SheepApp.getInstance(), onInitListener);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 朗读文本
|
|
|
+ *
|
|
|
+ * @param msg 文本信息
|
|
|
+ */
|
|
|
+ public void speakMsg(String msg) {
|
|
|
+ if(textToSpeech == null) {
|
|
|
+ this.msg = msg;
|
|
|
+ initTextToSpeech();
|
|
|
+ } else if (canSpeakNow()) {
|
|
|
+ try {
|
|
|
+ int result = textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
|
|
|
+ if(TestUtil.isDev())
|
|
|
+ G.showToast("result = " + result);
|
|
|
+ if(result != 0){
|
|
|
+ stopAndShutdownTTS();
|
|
|
+ speakMsg(msg);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ if(TestUtil.isDev())
|
|
|
+ G.showToast(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 朗读文本
|
|
|
+ *
|
|
|
+ * @param msg 文本信息
|
|
|
+ * @param pitch 音调 值越大声音越尖(女生),值越小则变成男声,1.0是常规
|
|
|
+ */
|
|
|
+ public void speakMsg(String msg, float pitch) {
|
|
|
+ if(textToSpeech == null) {
|
|
|
+ this.pitch = pitch;
|
|
|
+ this.msg = msg;
|
|
|
+ initTextToSpeech();
|
|
|
+ } else if (canSpeakNow()) {
|
|
|
+ try {
|
|
|
+ textToSpeech.setPitch(pitch);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
|
|
|
+ int result = textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
|
|
|
+ if(TestUtil.isDev())
|
|
|
+ G.showToast("result = " + result);
|
|
|
+ if(result != 0){
|
|
|
+ stopAndShutdownTTS();
|
|
|
+ speakMsg(msg, pitch);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ if(TestUtil.isDev())
|
|
|
+ G.showToast(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前可以朗读文本
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean canSpeakNow() {
|
|
|
+ return textToSpeech != null && !textToSpeech.isSpeaking();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stopAndShutdownTTS() {
|
|
|
+ try {
|
|
|
+ if (textToSpeech != null) {
|
|
|
+ textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
|
|
|
+ textToSpeech.shutdown(); // 关闭,释放资源
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //释放TTS
|
|
|
+ public static void stopTTS(){
|
|
|
+ if(instance != null){
|
|
|
+ instance.stopAndShutdownTTS();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|