You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.3 KiB
Java

package com.supervision.springaidemo.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.supervision.springaidemo.dto.paddlespeech.req.TtsReqDTO;
import com.supervision.springaidemo.dto.paddlespeech.res.PaddleSpeechResDTO;
import com.supervision.springaidemo.dto.paddlespeech.res.TtsResultDTO;
public class TtsUtil {
private static final String TTS_URL = "http://192.168.10.137:8090/paddlespeech/tts";
private static final ObjectMapper objectMapper = new ObjectMapper();
public static String ttsTransform(String str) {
// 构建
String post = HttpUtil.post(TTS_URL, JSONUtil.toJsonStr(new TtsReqDTO(str)));
try {
PaddleSpeechResDTO<TtsResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<TtsResultDTO>>() {
});
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new RuntimeException("文字转换语音失败");
}
return response.getResult().getAudio();
} catch (Exception e) {
throw new RuntimeException("语音转换文字失败", e);
}
}
}