|
|
@@ -11,7 +11,6 @@ import okhttp3.Response;
|
|
|
import okhttp3.WebSocket;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
import org.thyy.thirdpartapi.tts.Tts;
|
|
|
import org.thyy.thirdpartapi.tts.TtsConfig;
|
|
|
import org.thyy.thirdpartapi.tts.TtsRequest;
|
|
|
@@ -25,21 +24,24 @@ import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
@Slf4j
|
|
|
-@Service(value = "xf")
|
|
|
@Configuration
|
|
|
-@ConditionalOnProperty(prefix = "tts", name = "service", matchIfMissing = true, havingValue = "xf")
|
|
|
+@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) {
|
|
|
@@ -49,46 +51,50 @@ public class XfTtsApi implements Tts {
|
|
|
|
|
|
|
|
|
public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
|
|
|
+
|
|
|
+ if (ttsClient == null) {
|
|
|
+ return R.fail(ExceptionEnum.LOGICAL_ERROR, "未发现服务");
|
|
|
+ }
|
|
|
+
|
|
|
String filePath = cfg.getDirectory() + File.separator + request.getId() + ".mp3";
|
|
|
FileUtil.del(filePath);
|
|
|
|
|
|
CompletableFuture<Void> future = new CompletableFuture<>();
|
|
|
|
|
|
-// try {
|
|
|
-// File file = new File(filePath);
|
|
|
-// ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
|
|
|
-// @Override
|
|
|
-// public void onSuccess(byte[] bytes) {
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
|
|
|
-// log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
|
|
|
-// log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void onClosing(WebSocket webSocket, int code, String reason) {
|
|
|
-// super.onClosing(webSocket, code, reason);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void onClosed(WebSocket webSocket, int code, String reason) {
|
|
|
-// super.onClosed(webSocket, code, reason);
|
|
|
-// future.complete(null);
|
|
|
-// }
|
|
|
-// });
|
|
|
-// } catch (Exception e) {
|
|
|
-// log.error("生成语音文件失败", e);
|
|
|
-// log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
|
|
|
-// future.completeExceptionally(e);
|
|
|
-// }
|
|
|
-
|
|
|
- future.complete(null);
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(byte[] bytes) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
|
|
|
+ log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
|
|
|
+ log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClosing(WebSocket webSocket, int code, String reason) {
|
|
|
+ super.onClosing(webSocket, code, reason);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClosed(WebSocket webSocket, int code, String reason) {
|
|
|
+ super.onClosed(webSocket, code, reason);
|
|
|
+ future.complete(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("生成语音文件失败", e);
|
|
|
+ log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
|
|
|
+ future.completeExceptionally(e);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
String voiceUrl = cfg.getSpeechUrl() + "/" + request.getId() + ".mp3";
|
|
|
AtomicReference<ResultVo<String>> resultVo = new AtomicReference<>(R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, ""));
|