package com.example.nativetts;
|
|
import android.content.Context;
|
import android.speech.tts.TextToSpeech;
|
import android.speech.tts.UtteranceProgressListener;
|
import android.util.Log;
|
import java.util.Locale;
|
import org.json.JSONObject;
|
import io.dcloud.feature.expand.unimodule.UniModule;
|
import io.dcloud.b;
|
|
public class NativeTTSModule extends UniModule {
|
private static final String TAG = "NativeTTS";
|
private TextToSpeech mTts = null;
|
private boolean isInitialized = false;
|
private b moduleContext = null;
|
|
public NativeTTSModule(b reactContext) {
|
super(reactContext);
|
this.moduleContext = reactContext;
|
Context context = reactContext.getContext();
|
if (context != null) {
|
initTTS(context);
|
}
|
}
|
|
private void initTTS(Context context) {
|
mTts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
@Override
|
public void onInit(int status) {
|
if (status == TextToSpeech.SUCCESS) {
|
int result = mTts.setLanguage(Locale.CHINA);
|
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
|
Log.e(TAG, "TTS 语言不支持");
|
isInitialized = false;
|
} else {
|
isInitialized = true;
|
mTts.setSpeechRate(1.0f);
|
mTts.setPitch(1.0f);
|
Log.d(TAG, "TTS 初始化成功");
|
}
|
} else {
|
Log.e(TAG, "TTS 初始化失败");
|
isInitialized = false;
|
}
|
}
|
});
|
}
|
|
@Override
|
public String getModuleName() {
|
return "native-tts";
|
}
|
|
@Override
|
public JSONObject exec(JSONObject params, String callbackId) {
|
String method = "";
|
try {
|
method = params.getString("method");
|
Log.d(TAG, "exec method: " + method);
|
|
if ("init".equals(method)) {
|
JSONObject result = new JSONObject();
|
result.put("status", isInitialized ? 1 : 0);
|
result.put("message", isInitialized ? "初始化成功" : "初始化失败");
|
return result;
|
|
} else if ("speak".equals(method)) {
|
String text = params.getString("text");
|
speakText(text, callbackId);
|
|
} else if ("stop".equals(method)) {
|
stopSpeak();
|
JSONObject result = new JSONObject();
|
result.put("status", 0);
|
result.put("message", "已停止");
|
return result;
|
|
} else if ("setSpeed".equals(method)) {
|
double speed = params.optDouble("speed", 1.0);
|
if (mTts != null) {
|
mTts.setSpeechRate((float) speed);
|
}
|
JSONObject result = new JSONObject();
|
result.put("status", 0);
|
return result;
|
|
} else if ("setPitch".equals(method)) {
|
double pitch = params.optDouble("pitch", 1.0);
|
if (mTts != null) {
|
mTts.setPitch((float) pitch);
|
}
|
JSONObject result = new JSONObject();
|
result.put("status", 0);
|
return result;
|
|
} else if ("destroy".equals(method)) {
|
destroyTTS();
|
JSONObject result = new JSONObject();
|
result.put("status", 0);
|
result.put("message", "已销毁");
|
return result;
|
|
} else if ("isReady".equals(method)) {
|
JSONObject result = new JSONObject();
|
result.put("status", isInitialized ? 1 : 0);
|
return result;
|
|
} else if ("isSpeaking".equals(method)) {
|
JSONObject result = new JSONObject();
|
result.put("status", (mTts != null && mTts.isSpeaking()) ? 1 : 0);
|
return result;
|
}
|
} catch (Exception e) {
|
Log.e(TAG, "exec error: " + e.getMessage());
|
}
|
return null;
|
}
|
|
private void speakText(String text, final String callbackId) {
|
if (!isInitialized || mTts == null) {
|
try {
|
JSONObject result = new JSONObject();
|
result.put("status", -1);
|
result.put("message", "TTS 未初始化");
|
sendCallback(callbackId, result);
|
} catch (Exception e) {}
|
return;
|
}
|
|
if (text == null || text.isEmpty()) {
|
try {
|
JSONObject result = new JSONObject();
|
result.put("status", -1);
|
result.put("message", "文本不能为空");
|
sendCallback(callbackId, result);
|
} catch (Exception e) {}
|
return;
|
}
|
|
if (mTts.isSpeaking()) {
|
mTts.stop();
|
}
|
|
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
@Override
|
public void onStart(String utteranceId) {
|
Log.d(TAG, "TTS 开始: " + text);
|
}
|
|
@Override
|
public void onDone(String utteranceId) {
|
Log.d(TAG, "TTS 完成");
|
try {
|
JSONObject result = new JSONObject();
|
result.put("status", 0);
|
result.put("message", "播报完成");
|
sendCallback(callbackId, result);
|
} catch (Exception e) {}
|
}
|
|
@Override
|
public void onError(String utteranceId) {
|
Log.e(TAG, "TTS 错误");
|
try {
|
JSONObject result = new JSONObject();
|
result.put("status", -1);
|
result.put("message", "TTS 播报失败");
|
sendCallback(callbackId, result);
|
} catch (Exception e) {}
|
}
|
});
|
|
String utteranceId = "tts_" + System.currentTimeMillis();
|
int resultCode = mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
|
|
if (resultCode == TextToSpeech.ERROR) {
|
try {
|
JSONObject result = new JSONObject();
|
result.put("status", -1);
|
result.put("message", "TTS 启动失败");
|
sendCallback(callbackId, result);
|
} catch (Exception e) {}
|
}
|
}
|
|
private void stopSpeak() {
|
if (mTts != null && mTts.isSpeaking()) {
|
mTts.stop();
|
Log.d(TAG, "TTS 停止");
|
}
|
}
|
|
private void destroyTTS() {
|
if (mTts != null) {
|
mTts.stop();
|
mTts.shutdown();
|
mTts = null;
|
}
|
isInitialized = false;
|
Log.d(TAG, "TTS 销毁");
|
}
|
|
private void sendCallback(String callbackId, JSONObject result) {
|
if (callbackId != null && moduleContext != null) {
|
try {
|
moduleContext.a(callbackId, result);
|
} catch (Exception e) {
|
Log.e(TAG, "sendCallback error: " + e.getMessage());
|
}
|
}
|
}
|
}
|