Browse Source

<加入TTS语音播报,需要先安装TTS服务>

weizhengliang 4 năm trước cách đây
mục cha
commit
1aa82168fd

+ 0 - 1
app/src/main/java/com/wdkl/callingmainnurse/MyApplication.java

@@ -157,6 +157,5 @@ public class MyApplication extends Application {
     public void onTerminate() { // 程序终止 (Waderson 20180123)
         super.onTerminate();
         //unbindService(connection);
-        SpeechUtil.getInstance().release();
     }
 }

+ 2 - 0
app/src/main/java/com/wdkl/callingmainnurse/common/Constants.java

@@ -87,6 +87,8 @@ public class Constants {
     public static final String APP_CHECK_UPDATE = "/WDHS-I/callingMainNurse_APP_CheckUpdate.aspx";
     //public static final String APP_CHECK_UPDATE = "";
 
+    //tts服务是否可用: 0-不可用, 1-可用但不支持中文, 2-可用且支持中文
+    public static int TTS_STATUS = 0;
 
     //呼叫状态
     public static String CALL_STATE = "-1";

+ 4 - 0
app/src/main/java/com/wdkl/callingmainnurse/ui/activity/InitActivity.java

@@ -115,6 +115,10 @@ public class InitActivity extends BaseActivity implements SerialPortUtil.IForBro
         serialPortUtil.setOnBroadcastClickListener(this);
         serialPortUtil.setOnDataReceiveStringListener(this);
 
+        if (-100 == SharedPreferencesUtil.getIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.TtsOn)) {
+            SharedPreferencesUtil.putIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.TtsOn, 0);
+        }
+
         if (-100 == SharedPreferencesUtil.getIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.DoctorTransferTimeout)) {
             Constants.doctorTransferTimeout = 30;
             SharedPreferencesUtil.putIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.DoctorTransferTimeout, 30);

+ 2 - 0
app/src/main/java/com/wdkl/callingmainnurse/ui/activity/MainFragmentActivity.java

@@ -46,6 +46,7 @@ import com.wdkl.callingmainnurse.util.PopupwindowVoiceNoticeUtil;
 import com.wdkl.callingmainnurse.util.SharedPreferencesUtil;
 import com.wdkl.callingmainnurse.util.SipUtil.SipCallBackI;
 import com.wdkl.callingmainnurse.util.SipUtil.SipHelperUtil;
+import com.wdkl.callingmainnurse.util.SpeechUtil;
 import com.wdkl.callingmainnurse.util.StringUtils;
 import com.wdkl.callingmainnurse.util.TimeUtil;
 import com.wdkl.callingmainnurse.util.ToastUtil;
@@ -667,6 +668,7 @@ public class MainFragmentActivity extends FragmentActivity implements IVvsipServ
         unregisterReceiver(networkConnectChangedReceiver);
         //关闭Sip服务
         SipHelperUtil.getInstance(this).unRegisterSip();
+        SpeechUtil.getInstance().release();
     }
 
     /**

+ 29 - 0
app/src/main/java/com/wdkl/callingmainnurse/ui/activity/OtherSettingsActivity.java

@@ -52,6 +52,12 @@ public class OtherSettingsActivity extends BaseActivity {
     @Bind(R.id.activity_other_layout_tb)
     Toolbar toolbar;
 
+    @Bind(R.id.group_tts_set)
+    RadioGroup groupTtsSet;
+    @Bind(R.id.rb_tts_on)
+    RadioButton rbTtsOn;
+    @Bind(R.id.rb_tts_off)
+    RadioButton rbTtsOff;
     @Bind(R.id.group_doctor_device_set)
     RadioGroup groupDoctorDevice;
     @Bind(R.id.rb_transfer_doctor_on)
@@ -95,6 +101,29 @@ public class OtherSettingsActivity extends BaseActivity {
 
     @Override
     protected void initView() {
+        int tts_status = SharedPreferencesUtil.getIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.TtsOn);
+        if (tts_status == 1) {
+            rbTtsOn.setChecked(true);
+        } else {
+            rbTtsOff.setChecked(true);
+        }
+        groupTtsSet.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(RadioGroup group, int checkedId) {
+                if (checkedId == R.id.rb_tts_on) {
+                    if (Constants.TTS_STATUS == 2) {
+                        ToastUtil.showToast("设置成功,请重启设备!");
+                        SharedPreferencesUtil.putIntSp(OtherSettingsActivity.this, Constants.MSG_SP, SharedPreferencesUtil.TtsOn, 1);
+                    } else {
+                        ToastUtil.showToast("设置失败,请先安装TTS服务!");
+                        rbTtsOff.setChecked(true);
+                    }
+                } else {
+                    SharedPreferencesUtil.putIntSp(OtherSettingsActivity.this, Constants.MSG_SP, SharedPreferencesUtil.TtsOn, 0);
+                }
+            }
+        });
+
         int status = SharedPreferencesUtil.getIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.DoctorOn);
         if (status == 1) {
             rbDoctorOn.setChecked(true);

+ 20 - 4
app/src/main/java/com/wdkl/callingmainnurse/ui/fragment/CallingBedFragment.java

@@ -426,7 +426,8 @@ public class CallingBedFragment extends BaseFragment implements SicknessBedAdapt
 
     public static CallingBedFragment mCallingBedFragment;
 
-    private boolean enableTTS = false;
+    //是否开启TTS语音播报
+    private boolean enableTTS = (Constants.TTS_STATUS == 2 && SharedPreferencesUtil.getIntSp(MyApplication.getAppContext(), Constants.MSG_SP, SharedPreferencesUtil.TtsOn)==1);
 
     private CallATimeoutThread callATimeoutThread;
     private List<CallListEntity> callListEntityList;
@@ -520,7 +521,9 @@ public class CallingBedFragment extends BaseFragment implements SicknessBedAdapt
         serialPortUtil.setOnDataReceiveListener(this, this);
         setVolumePA(false);
         setPowerLed("1");
-        if (!enableTTS) {
+        if (enableTTS) {
+            SpeechUtil.getInstance().startSpeechThread();
+        } else {
             startPalyPhonetics();
         }
     }
@@ -1861,6 +1864,11 @@ public class CallingBedFragment extends BaseFragment implements SicknessBedAdapt
 
     //语音播报
     private void startSpeak(UdpEntity entity) {
+        String tag = SharedPreferencesUtil.getStringSp(getActivity(), Constants.MSG_SP, Constants.VOICE_BROADCAST_TAG);
+        if ("2".equals(tag)) {
+            return;
+        }
+
         loopingUdpentity = entity;
         showUrgentWindow(entity);
         String type = entity.getType();
@@ -1878,11 +1886,19 @@ public class CallingBedFragment extends BaseFragment implements SicknessBedAdapt
                 break;
             case Constants.SON_CALL://床位
             case Constants.TRADITION_CALL://传统床位
-                text = rnb + "房" + bnb + "床呼叫";
+                if ("1".equals(tag)) {//是否要播报“房” 0不要  1要
+                    text = rnb + "房" + bnb + "床呼叫";
+                } else {
+                    text = bnb + "床呼叫";
+                }
                 SpeechUtil.getInstance().newSpeech(text);
                 break;
             case Constants.ROOMHELP_CALL://请求增援
-                text = rnb + "房" + bnb + "床请求增援";
+                if ("1".equals(tag)) {//是否要播报“房” 0不要  1要
+                    text = rnb + "房" + bnb + "床请求增援";
+                } else {
+                    text = bnb + "床请求增援";
+                }
                 SpeechUtil.getInstance().newSpeech(text);
                 break;
             case Constants.WSHROOM_CALL://卫生间

+ 1 - 0
app/src/main/java/com/wdkl/callingmainnurse/util/SharedPreferencesUtil.java

@@ -26,6 +26,7 @@ public class SharedPreferencesUtil {
     public static final String DoctorOn = "DoctorOn";
     public static final String SlaveDeviceOn = "SlaveDeviceOn";
     public static final String HostDeviceId = "HostDeviceId";
+    public static final String TtsOn = "TtsOn";
 
     private static SharedPreferencesUtil mSharedPreferencesUtil;
 

+ 59 - 16
app/src/main/java/com/wdkl/callingmainnurse/util/SpeechUtil.java

@@ -4,6 +4,10 @@ import android.content.Context;
 import android.speech.tts.TextToSpeech;
 import android.speech.tts.UtteranceProgressListener;
 
+import com.wdkl.callingmainnurse.common.Constants;
+
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 
 public class SpeechUtil {
@@ -13,8 +17,10 @@ public class SpeechUtil {
     private static SpeechUtil speech;
     private int speakIndex = 0;
     private int loopCount = 2;
-    private String speechText;
     private boolean isStop = true;
+    private static List<String> speechTextList = new ArrayList<>();
+    private Thread speechThread;
+    private boolean isSpeechLoop = true;
 
     public static SpeechUtil getInstance() {
         if (speech == null) {
@@ -34,13 +40,15 @@ public class SpeechUtil {
                 if (status == TextToSpeech.SUCCESS) {
                     int supported = textToSpeech.setLanguage(Locale.CHINESE);
                     if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
-                        //ToastUtil.showToast("当前不支持中文!");
+                        Constants.TTS_STATUS = 1;
                         LogUtil.d(TAG, "onInit: 当前不支持中文");
                     } else {
+                        Constants.TTS_STATUS = 2;
                         LogUtil.d(TAG, "onInit: 支持中文");
                     }
                     LogUtil.d(TAG, "onInit: TTS引擎初始化成功");
                 } else {
+                    Constants.TTS_STATUS = 0;
                     LogUtil.d(TAG, "onInit: TTS引擎初始化失败");
                 }
             }
@@ -49,18 +57,15 @@ public class SpeechUtil {
     }
 
     public void newSpeech(String text) {
-        LogUtil.d(TAG, "start speech: " + text);
-        if (loopCount > 0) {
-            stopSpeak();
-            speechText = text;
-            speakIndex = 0;
-            speak();
-            isStop = false;
-        }
+        //LogUtil.d(TAG, "add text speech: " + text);
+        startSpeechThread();
+        speechTextList.add(text);
     }
 
-    public void speak() {
-        textToSpeech.speak(speechText, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
+    public synchronized void speak(final String text) {
+        //LogUtil.d(TAG, "start speak");
+        isStop = false;
+        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
         textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
             @Override
             public void onStart(String utteranceId) {
@@ -71,8 +76,13 @@ public class SpeechUtil {
             public void onDone(String utteranceId) {
                 speakIndex++;
                 //LogUtil.d(TAG, "speak onDone...index: " + speakIndex + ", loop: " + loopCount);
-                if (speakIndex < loopCount && !isStop) {
-                    speak();
+                if (speakIndex < loopCount) {
+                    //循环播报
+                    speak(text);
+                } else {
+                    //语音播报完毕
+                    speakIndex = 0;
+                    isStop = true;
                 }
             }
 
@@ -85,9 +95,10 @@ public class SpeechUtil {
 
     public void stopSpeak() {
         if (textToSpeech.isSpeaking()) {
-            isStop = true;
             textToSpeech.stop();
-            LogUtil.d(TAG, "stop speak");
+            speechTextList.clear();
+            isStop = true;
+            //LogUtil.d(TAG, "stop speak");
         }
     }
 
@@ -96,6 +107,8 @@ public class SpeechUtil {
     }
 
     public void release() {
+        speechTextList.clear();
+        isStop = true;
         if (textToSpeech != null) {
             textToSpeech.stop();
             textToSpeech.shutdown();
@@ -103,4 +116,34 @@ public class SpeechUtil {
         }
     }
 
+    public void startSpeechThread() {
+        if (null == speechThread) {
+            speechThread = new Thread(new SpeechRunnable());
+            speechThread.start();
+        } else if (!speechThread.isAlive()) {
+            speechThread.start();
+        }
+    }
+
+    public class SpeechRunnable implements Runnable {
+        public void run() {
+            while (isSpeechLoop) {
+                if (speechTextList.size() > 0 && isStop) {
+                    String speech = speechTextList.get(0);
+                    speak(speech);
+
+                    if (speechTextList.contains(speech)) {
+                        speechTextList.remove(speech);
+                    }
+
+                    try {
+                        Thread.sleep(50);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+    }
+
 }

+ 37 - 0
app/src/main/res/layout/activity_other_settings_layout.xml

@@ -22,6 +22,43 @@
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
+            android:text="使用TTS语音播报:"
+            android:textColor="@color/white"
+            android:textSize="28sp"/>
+
+        <RadioGroup
+            android:id="@+id/group_tts_set"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginLeft="30dp"
+            android:orientation="horizontal">
+            <RadioButton
+                android:id="@+id/rb_tts_on"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="是"
+                android:textColor="@color/white"
+                android:textSize="28sp"/>
+            <RadioButton
+                android:id="@+id/rb_tts_off"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginLeft="60dp"
+                android:text="否"
+                android:textColor="@color/white"
+                android:textSize="28sp"/>
+        </RadioGroup>
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="80dp"
+        android:padding="20dp"
+        android:gravity="center_vertical">
+
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
             android:text="分机呼叫无人处理时转接:"
             android:textColor="@color/white"
             android:textSize="28sp"/>