|
@@ -1,6 +1,7 @@
|
|
|
package com.wdkl.ncs.android.component.nursehome.util;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.os.Build;
|
|
|
import android.speech.tts.TextToSpeech;
|
|
|
import android.speech.tts.UtteranceProgressListener;
|
|
|
import android.text.TextUtils;
|
|
@@ -45,66 +46,102 @@ public class SpeechUtil {
|
|
|
public void init(Context context, final TtsCallback callback) {
|
|
|
final String language = LocaleMangerUtils.getApplicationLocale().getLanguage();
|
|
|
Log.d(TAG, "app locale language: " + language);
|
|
|
- if (Locale.CHINESE.getLanguage().equals(language)) {
|
|
|
- textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
- @Override
|
|
|
- public void onInit(int status) {
|
|
|
- if (status == TextToSpeech.SUCCESS) {
|
|
|
- int supported = textToSpeech.setLanguage(Locale.CHINESE);
|
|
|
- if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
- Constant.TTS_STATUS=1;
|
|
|
- Log.d(TAG, "onInit: 当前不支持中文");
|
|
|
+ //8.0以下版本自动设置tts
|
|
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O ) {
|
|
|
+ if (Locale.CHINESE.getLanguage().equals(language)) {
|
|
|
+ textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
+ @Override
|
|
|
+ public void onInit(int status) {
|
|
|
+ if (status == TextToSpeech.SUCCESS) {
|
|
|
+ int supported = textToSpeech.setLanguage(Locale.CHINESE);
|
|
|
+ if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
+ Constant.TTS_STATUS = 1;
|
|
|
+ Log.d(TAG, "onInit: 当前不支持中文");
|
|
|
+ if (callback != null) {
|
|
|
+ callback.onInit(false);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Constant.TTS_STATUS = 2;
|
|
|
+ Log.d(TAG, "onInit: 支持中文");
|
|
|
+ if (callback != null) {
|
|
|
+ callback.onInit(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
+ } else {
|
|
|
+ Constant.TTS_STATUS = 0;
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
if (callback != null) {
|
|
|
callback.onInit(false);
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, "com.iflytek.speechcloud");
|
|
|
+ textToSpeech.setSpeechRate(0.5f);
|
|
|
+ } else {
|
|
|
+ textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
+ @Override
|
|
|
+ public void onInit(int status) {
|
|
|
+ if (status == TextToSpeech.SUCCESS) {
|
|
|
+ int supported = textToSpeech.setLanguage(new Locale(language));
|
|
|
+ if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
+ Constant.TTS_STATUS = 1;
|
|
|
+ Log.d(TAG, "onInit: 当前不支持" + language);
|
|
|
+ if (callback != null) {
|
|
|
+ callback.onInit(false);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Constant.TTS_STATUS = 2;
|
|
|
+ Log.d(TAG, "onInit: 支持" + language);
|
|
|
+ if (callback != null) {
|
|
|
+ callback.onInit(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
} else {
|
|
|
- Constant.TTS_STATUS=2;
|
|
|
- Log.d(TAG, "onInit: 支持中文");
|
|
|
+ Constant.TTS_STATUS = 0;
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
if (callback != null) {
|
|
|
- callback.onInit(true);
|
|
|
+ callback.onInit(false);
|
|
|
}
|
|
|
}
|
|
|
- Log.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
- } else {
|
|
|
- Constant.TTS_STATUS=0;
|
|
|
- Log.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
- if (callback != null) {
|
|
|
- callback.onInit(false);
|
|
|
- }
|
|
|
}
|
|
|
- }
|
|
|
- }, "com.iflytek.speechcloud");
|
|
|
- textToSpeech.setSpeechRate(0.5f);
|
|
|
+ }, "com.google.android.tts");
|
|
|
+ textToSpeech.setSpeechRate(0.8f);
|
|
|
+ }
|
|
|
} else {
|
|
|
+ //8.0以上版本需要手动设置tts
|
|
|
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
@Override
|
|
|
public void onInit(int status) {
|
|
|
if (status == TextToSpeech.SUCCESS) {
|
|
|
int supported = textToSpeech.setLanguage(new Locale(language));
|
|
|
if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
- Constant.TTS_STATUS=1;
|
|
|
+ Constant.TTS_STATUS = 1;
|
|
|
Log.d(TAG, "onInit: 当前不支持" + language);
|
|
|
if (callback != null) {
|
|
|
callback.onInit(false);
|
|
|
}
|
|
|
} else {
|
|
|
- Constant.TTS_STATUS=2;
|
|
|
+ Constant.TTS_STATUS = 2;
|
|
|
Log.d(TAG, "onInit: 支持" + language);
|
|
|
if (callback != null) {
|
|
|
callback.onInit(true);
|
|
|
}
|
|
|
+ if (!Locale.CHINESE.getLanguage().equals(language)) {
|
|
|
+ textToSpeech.setSpeechRate(0.9f);
|
|
|
+ }
|
|
|
}
|
|
|
Log.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
} else {
|
|
|
- Constant.TTS_STATUS=0;
|
|
|
+ Constant.TTS_STATUS = 0;
|
|
|
Log.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
if (callback != null) {
|
|
|
callback.onInit(false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }, "com.google.android.tts");
|
|
|
- textToSpeech.setSpeechRate(0.8f);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
}
|