[Feat]增加Tts回调

This commit is contained in:
chenfufeng
2022-10-25 16:10:52 +08:00
parent a718188b99
commit 7e0a85073f
3 changed files with 28 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import android.content.Context;
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger;
import com.mogo.eagle.core.utilcode.util.ToastUtils;
import com.mogo.tts.base.IMogoTTS;
import com.mogo.tts.base.IMogoTTSCallback;
import com.mogo.tts.base.PreemptType;
/**
@@ -90,6 +91,12 @@ public class AIAssist {
}
}
public void speakTTSVoiceWithLevel(String text, int level, IMogoTTSCallback callBack) {
if (mTTS != null) {
mTTS.speakTTSVoiceWithLevel(text, level, callBack);
}
}
/**
* 等级由低到高为0、1、2、3分别对应p3、p2、p1、p0
* @param text

View File

@@ -41,6 +41,8 @@ interface IMogoTTS extends IProvider {
void speakTTSVoiceWithLevel( String tts, int level);
void speakTTSVoiceWithLevel( String tts, int level, IMogoTTSCallback callBack);
/**
* 播放 tts 语音
*

View File

@@ -63,6 +63,8 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
// 等级由低到高为0、1、2、3默认为-1表示没有正在tts的
private int curTtsLevel = -1;
// 由于主动打断不会有回调事件所以主动打断时清掉map中被打断的text和callback
private String curTtsContent = "";
private LinkedList<Pair<String, Integer>> linkedList = new LinkedList<>();
public void release() {
@@ -79,6 +81,7 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
}
mQAndAMap.clear();
mVoiceClient.release();
curTtsContent = "";
curTtsLevel = -1;
if (mEngine != null) {
mEngine.destroy();
@@ -351,6 +354,7 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
if (mSpeakVoiceMap.containsKey(text)) {
mSpeakVoiceMap.remove(text);
}
curTtsContent = "";
curTtsLevel = -1;
mEngine.stop();
}
@@ -358,7 +362,11 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
public void stopTts() {
if (mEngine != null && mHasAuth) {
if (mSpeakVoiceMap.containsKey(curTtsContent)) {
mSpeakVoiceMap.remove(curTtsContent);
}
// tts过程中调用stop不会有回调事件
curTtsContent = "";
curTtsLevel = -1;
mEngine.stop();
}
@@ -410,12 +418,21 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
}
}
curTtsLevel = ttsLevel;
curTtsContent = text;
// 合成并播放
CallerLogger.INSTANCE.d(TAG, "tts准备合成" + text + ",curTtsLevel为" + curTtsLevel);
mEngine.speak(text, text, mAILocalTTSIntent);
}
}
@MainThread
public void speakTTSVoiceWithLevel(String text, int ttsLevel, IMogoTTSCallback callBack) {
if (mHasFlush && mEngine != null) {
mSpeakVoiceMap.put(text, callBack);
}
speakTTSVoiceWithLevel(text, ttsLevel);
}
// 降序插入Tts(目前Level0、1可排队)
private void insertTts(String text, int level) {
int index = -1;
@@ -741,6 +758,7 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
public void onError(String utteranceId, AIError aiError) {
CallerLogger.INSTANCE.d(TAG, "检测到错误:" + aiError.toString());
curTtsLevel = -1;
curTtsContent = "";
IMogoTTSCallback callBack = PadTTS.this.mSpeakVoiceMap.remove(utteranceId);
if (callBack != null) {
callBack.onSpeakError(utteranceId, aiError.getError());
@@ -755,6 +773,7 @@ public class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsList
@Override
public void onCompletion(String utteranceId) {
curTtsLevel = -1;
curTtsContent = "";
CallerLogger.INSTANCE.d(TAG, "播放完成");
ttsNext();
IMogoTTSCallback callBack = PadTTS.this.mSpeakVoiceMap.remove(utteranceId);