|
@@ -5,11 +5,11 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import cn.xfyun.api.TtsClient;
|
|
|
import cn.xfyun.model.response.TtsResponse;
|
|
|
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.Response;
|
|
|
import okhttp3.WebSocket;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
import org.thyy.thirdpartapi.tts.Tts;
|
|
|
import org.thyy.thirdpartapi.tts.TtsConfig;
|
|
|
import org.thyy.thirdpartapi.tts.TtsRequest;
|
|
@@ -23,19 +23,24 @@ import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
@Slf4j
|
|
|
-@Service(value = "xf")
|
|
|
+@Configuration
|
|
|
+@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "xfyun")
|
|
|
public class XfTtsApi implements Tts {
|
|
|
private TtsClient ttsClient;
|
|
|
- private TtsConfig cfg;
|
|
|
+ private final TtsConfig cfg;
|
|
|
|
|
|
- public void init(TtsConfig cfg, TtsConfig.Config xf) {
|
|
|
+ public XfTtsApi(TtsConfig cfg) {
|
|
|
+ this.cfg = cfg;
|
|
|
+ init();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void init() {
|
|
|
try {
|
|
|
- this.cfg = cfg;
|
|
|
if (StrUtil.isBlank(cfg.getDirectory())) {
|
|
|
cfg.setDirectory(System.getProperty("user.dir"));
|
|
|
}
|
|
|
ttsClient = new TtsClient.Builder()
|
|
|
- .signature(xf.getAppId(), xf.getApiKey(), xf.getApiSecret())
|
|
|
+ .signature(cfg.getAppId(), cfg.getApiKey(), cfg.getApiSecret())
|
|
|
.vcn("aisjinger").volume(100).build();
|
|
|
log.info("讯飞SDK初始化成功。");
|
|
|
} catch (Exception e) {
|
|
@@ -44,7 +49,7 @@ public class XfTtsApi implements Tts {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
|
|
|
+ public ResultVo<String> textToSpeech(TtsRequest request) {
|
|
|
String filePath = cfg.getDirectory() + File.separator + request.getId() + ".mp3";
|
|
|
FileUtil.del(filePath);
|
|
|
|
|
@@ -84,6 +89,7 @@ public class XfTtsApi implements Tts {
|
|
|
future.completeExceptionally(e);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
String voiceUrl = cfg.getSpeechUrl() + "/" + request.getId() + ".mp3";
|
|
|
AtomicReference<ResultVo<String>> resultVo = new AtomicReference<>(R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, ""));
|
|
|
|