This commit is contained in:
yangyakun
2026-04-09 19:06:36 +08:00
parent 2c4cf902af
commit 724fbbadb0
6 changed files with 108 additions and 118 deletions

View File

@@ -1,7 +1,11 @@
package com.mogo.tts
import android.content.Context
import com.mogo.tts.common.IGlobalTtsCallback
import com.mogo.tts.common.IMogoTTS
import com.mogo.tts.common.IMogoTTSCallback
import com.mogo.tts.common.LangTtsEntity
import com.mogo.tts.common.LanguageType
import com.mogo.tts.common.log.TtsLogManager
object TtsManager {
@@ -10,7 +14,7 @@ object TtsManager {
private var mTTS: IMogoTTS? = null
fun init(context: Context){
fun init(context: Context) {
try {
// 暂时换成反射,解决死锁问题
var clazz1: Class<*>? = null
@@ -26,18 +30,43 @@ object TtsManager {
mTTS?.initTts(context)
} catch (e: Exception) {
e.printStackTrace()
TtsLogManager.d(TAG,"TTS 模块初始化异常")
TtsLogManager.d(TAG, "TTS 模块初始化异常")
}
}
fun speakTTSVoice(tts:String){
mTTS?.speakTTSVoice(tts)
fun speakTTSVoice(tts: String?) {
if(tts.isNullOrEmpty()) return
speakTTSVoice(tts,null)
}
fun stopTTS(){
fun speakTTSVoice(tts: String?, callBack: IMogoTTSCallback?) {
if(tts.isNullOrEmpty()) return
speakTTSVoice(LangTtsEntity(tts, LanguageType.CHINESE), callBack);
}
fun speakTTSVoice(ttsEntity: LangTtsEntity, callBack: IMogoTTSCallback?) {
if(ttsEntity.ttsContent.isEmpty()){
return
}
mTTS?.speakTTSVoice(ttsEntity, callBack)
}
fun registerTtsListener(key: String, callback: IGlobalTtsCallback) {
mTTS?.registerTtsListener(key, callback)
}
fun unRegisterTtsListener(key: String) {
mTTS?.unRegisterTtsListener(key)
}
fun stopTTS() {
mTTS?.stopTts()
}
fun release() {
mTTS?.release()
}
}

View File

@@ -18,16 +18,12 @@ interface IMogoTTS {
*/
void release();
void speakTTSVoice(String tts);
void registerTtsListener(String key, IGlobalTtsCallback callback);
void speakTTSVoice(String tts, IMogoTTSCallback callBack);
void unRegisterTtsListener(String key);
void speakTTSVoice(LangTtsEntity ttsEntity, IMogoTTSCallback callBack);
void stopTts();
void registerTtsListener(IGlobalTtsCallback callback);
default void clearTTSCallback(String tts) {
}
}

View File

@@ -39,29 +39,6 @@ interface IMogoTTSCallback {
default void onTTSError( String ttsId, String tts ) {
}
/**
* 免唤醒命令响应回调
*
* @param cmd
*/
default void onCmdSelected( String cmd ) {
}
/**
* 语音播报临时免唤醒“确定”命令
*
* @param speakText 播报内容
*/
default void onCmdAction( String speakText ) {
}
/**
* 语音播报临时免唤醒“取消”命令
*
* @param speakText 播报内容
*/
default void onCmdCancel( String speakText ) {
}
default void onSpeakStart( String speakText ) {
}
@@ -80,11 +57,4 @@ interface IMogoTTSCallback {
default void onSpeakError( String speakText, String errorMsg) {
}
/**
* 语音播报完临时命令选择超时
*
* @param speakText 播报内容
*/
default void onSpeakSelectTimeOut( String speakText ) {
}
}

View File

@@ -8,7 +8,6 @@ import com.mogo.tts.common.IGlobalTtsCallback;
import com.mogo.tts.common.IMogoTTS;
import com.mogo.tts.common.IMogoTTSCallback;
import com.mogo.tts.common.LangTtsEntity;
import com.mogo.tts.common.LanguageType;
import com.mogo.tts.common.log.TtsLogManager;
import java.util.HashMap;
@@ -21,8 +20,7 @@ public abstract class BaseMogoTTS implements IMogoTTS {
protected volatile LangTtsEntity curTtsEntity = null;
protected HashMap<String, IMogoTTSCallback> speakVoiceMap = new HashMap<>();
protected IGlobalTtsCallback mGlobalTtsCallback = null;
protected HashMap<String, IGlobalTtsCallback> mGlobalTtsCallback = new HashMap<>();
protected String getTAG() {
return "BaseMogoTTS";
@@ -33,29 +31,24 @@ public abstract class BaseMogoTTS implements IMogoTTS {
this.context = context;
}
@Override
public void speakTTSVoice(String tts) {
if (tts.isEmpty()) return;
speakTTSVoice(tts,null);
public void registerTtsListener(String key,IGlobalTtsCallback callback) {
if (!mGlobalTtsCallback.containsKey(key)) {
this.mGlobalTtsCallback.put(key,callback);
}
}
@Override
public void speakTTSVoice(String tts, IMogoTTSCallback callBack) {
if (tts.isEmpty()) return;
speakTTSVoice(new LangTtsEntity(tts, LanguageType.CHINESE), callBack);
public void unRegisterTtsListener(String key){
this.mGlobalTtsCallback.remove(key);
}
@Override
public void speakTTSVoice(LangTtsEntity ttsEntity, IMogoTTSCallback callBack) {
if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
UiThreadHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
speakVoiceMap.put(ttsEntity.toString(),callBack);
}
speakMultiLangTTS(ttsEntity);
UiThreadHandler.post(() -> {
if (callBack != null) {
speakVoiceMap.put(ttsEntity.toString(),callBack);
}
speakMultiLangTTS(ttsEntity);
});
}else {
if (callBack != null) {
@@ -74,12 +67,7 @@ public abstract class BaseMogoTTS implements IMogoTTS {
@Override
public void stopTts() {
if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
UiThreadHandler.post(new Runnable() {
@Override
public void run() {
realStop();
}
});
UiThreadHandler.post(this::realStop);
} else {
realStop();
}
@@ -90,14 +78,64 @@ public abstract class BaseMogoTTS implements IMogoTTS {
if(curTtsEntity!=null){
String key = curTtsEntity.toString();
if (speakVoiceMap.containsKey(key)) {
speakVoiceMap.remove(key).onStopTts(key);
IMogoTTSCallback remove = speakVoiceMap.remove(key);
if(remove!=null) {
remove.onStopTts(key);
}
}
curTtsEntity = null;
}
this.curTtsContent = "";
}
public void registerTtsListener(IGlobalTtsCallback callback) {
this.mGlobalTtsCallback = callback;
public void onSpeakBegin() {
if(!mGlobalTtsCallback.isEmpty()) {
for (IGlobalTtsCallback callback : mGlobalTtsCallback.values()) {
callback.onTtsSpeakStart();
}
}
if (curTtsEntity!=null) {
String key = curTtsEntity.toString();
IMogoTTSCallback iMogoTTSCallback = speakVoiceMap.get(key);
if(iMogoTTSCallback!=null){
iMogoTTSCallback.onSpeakStart(key);
}
}
}
public void handleCompleteEvent() {
if(!mGlobalTtsCallback.isEmpty()) {
for (IGlobalTtsCallback callback : mGlobalTtsCallback.values()) {
callback.onTtsSpeakEnd();
}
}
if (curTtsEntity!=null) {
String key = curTtsEntity.toString();
IMogoTTSCallback iMogoTTSCallback = speakVoiceMap.get(key);
if(iMogoTTSCallback!=null){
iMogoTTSCallback.onSpeakEnd(key);
}
}
curTtsEntity = null;
curTtsContent = "";
}
public void handleErrorEvent(String error) {
if (curTtsEntity != null) {
String key = curTtsEntity.toString();
IMogoTTSCallback iMogoTTSCallback = speakVoiceMap.get(key);
if(iMogoTTSCallback!=null){
iMogoTTSCallback.onSpeakError(key,
error);
}
} else {
IMogoTTSCallback iMogoTTSCallback = speakVoiceMap.get(curTtsContent);
if(iMogoTTSCallback!=null){
iMogoTTSCallback.onSpeakError(curTtsContent,
error);
}
}
curTtsEntity = null;
curTtsContent = "";
}
}

View File

@@ -17,7 +17,6 @@ import com.iflytek.aikit.core.AiText
import com.iflytek.aikit.core.BaseLibrary
import com.iflytek.aikit.core.CoreListener
import com.iflytek.aikit.core.ErrType
import com.mogo.tts.common.IGlobalTtsCallback
import com.mogo.tts.common.LangTtsEntity
import com.mogo.tts.common.log.TtsLogManager
import com.mogo.tts.common.impl.BaseMogoTTS
@@ -97,7 +96,7 @@ class IFlyTekOfflineTts : BaseMogoTTS() {
private val completeListener = object : AudioTrackManager.OnCompleteListener {
override fun onComplete() {
onCompleted()
handleCompleteEvent()
}
}
@@ -183,29 +182,6 @@ class IFlyTekOfflineTts : BaseMogoTTS() {
}
}
private fun handleCompleteEvent() {
mGlobalTtsCallback?.onTtsSpeakEnd()
speakVoiceMap.remove(curTtsContent)?.onSpeakEnd(curTtsContent)
curTtsContent = ""
}
private fun handleErrorEvent(error: String) {
if (curTtsEntity != null) {
speakVoiceMap.remove(curTtsEntity.toString())
?.onSpeakError(
curTtsEntity.toString(),
error
)
} else {
speakVoiceMap.remove(curTtsContent)?.onSpeakError(
curTtsContent,
error
)
}
curTtsEntity = null
curTtsContent = ""
}
private val aiRespListener = object : AiListener {
override fun onResult(handleID: Int, list: MutableList<AiResponse>?, usrCxt: Any?) {
if (null != list && list.size > 0) {
@@ -275,29 +251,4 @@ class IFlyTekOfflineTts : BaseMogoTTS() {
handleErrorEvent(errorInfo)
}
}
private fun onSpeakBegin() {
mGlobalTtsCallback?.onTtsSpeakStart()
if (Thread.currentThread() == Looper.getMainLooper().thread) {
curTtsEntity?.let {
speakVoiceMap[it.toString()]?.onSpeakStart(it.toString())
}
} else {
UiThreadHandler.post {
curTtsEntity?.let {
speakVoiceMap[it.toString()]?.onSpeakStart(it.toString())
}
}
}
}
private fun onCompleted() {
if (Thread.currentThread() == Looper.getMainLooper().thread) {
handleCompleteEvent()
} else {
UiThreadHandler.post {
handleCompleteEvent()
}
}
}
}

View File

@@ -171,7 +171,9 @@ class MogoOfflineTTS : BaseMogoTTS() {
config = GenerationConfig(sid = 0, speed = 1.0f),
callback = object : Function1<FloatArray, Int> {
override fun invoke(samples: FloatArray): Int {
var isFirstStartPlay = false
if(lock.isLocked){
isFirstStartPlay = true
TtsLogManager.d(tag,"4 生成成功 去解锁")
lock.unlock()
}
@@ -188,6 +190,9 @@ class MogoOfflineTTS : BaseMogoTTS() {
return 0
}
if (!stopped) {
if(isFirstStartPlay){
onSpeakBegin()
}
track?.write(samples, 0, samples.size, AudioTrack.WRITE_BLOCKING)
return 1
} else {
@@ -198,6 +203,7 @@ class MogoOfflineTTS : BaseMogoTTS() {
}
)
TtsLogManager.d(tag,"5 realSpeak-结束播放语音")
handleCompleteEvent()
}finally {
if(lock.isLocked) {
TtsLogManager.d(tag,"realSpeak-finally 中发现还在锁定解锁")