语音识别、音频翻译案例
环境信息:
jdk java21
spring-ai-alibaba 版本为1.0.0.2
spring-ai 版本为 1.0.0
spring-boot 版本为 3.5.7
项目接入oss
1.依赖包
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.22.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
2.代码内容
import com.alibaba.dashscope.audio.asr.transcription.Transcription;
import com.alibaba.dashscope.audio.asr.transcription.TranscriptionParam;
import com.alibaba.dashscope.audio.asr.transcription.TranscriptionQueryParam;
import com.alibaba.dashscope.audio.asr.transcription.TranscriptionResult;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import com.pennon.agent.service.OssService; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
public class TextAudioConvertUtils {
//文本转语音模型
private static String model = "cosyvoice-v1";
// 音色
private static String voice = "longjielidou";
@Value("${spring.ai.dashscope.api-key}")
private String apiKey;
@Value("${upload.path.urlPrefix}")
private String urlPrefix;
@Autowired private OssService ossService;
//文本转语音
public Map<String, Object> getAudioOssUrl(String text, String sessionId, String batchNumber) {
// 请求参数
SpeechSynthesisParam param =
SpeechSynthesisParam.builder()
// 若没有将API Key配置到环境变量中,需将下面这行代码注释放开,并将your-api-key替换为自己的API Key
.apiKey(apiKey)
.model(model) // 模型
.voice(voice) // 音色
.build();
// 同步模式:禁用回调(第二个参数为null)
SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
ByteBuffer audio = null;
try {
// 阻塞直至音频返回
audio = synthesizer.call(text);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 任务结束关闭websocket连接
synthesizer.getDuplexApi().close(1000, "bye");
}
if (audio != null) {
// 首次发送文本时需建立 WebSocket 连接,因此首包延迟会包含连接建立的耗时
log.info(
"[Metric] requestId为:"
+ synthesizer.getLastRequestId()
+ "首包延迟(毫秒)为:"
+ synthesizer.getFirstPackageDelay());
//字节流语音转换
byte[] array = audio.array();
InputStream inputStream = new ByteArrayInputStream(array);
String fileName = sessionId + ":" + System.currentTimeMillis() + ":" + batchNumber + ".mp3";
//上传到oss
Map<String, Object> resultMap = ossService.uploadFile(inputStream, fileName, urlPrefix);
return resultMap;
}
return new HashMap<>();
}
// 语音翻译为文本
public String getTextByAudioOssUrl(String ossUrl) throws Exception {
TranscriptionParam param =
TranscriptionParam.builder()
// 若没有将API Key配置到环境变量中,需将apiKey替换为自己的API Key
.apiKey(apiKey)
.model("paraformer-v2")
// “language_hints”只支持paraformer-v2模型
.parameter("language_hints", new String[]{"zh", "en"})
.fileUrls(Arrays.asList(ossUrl))
.build();
Transcription transcription = new Transcription();
// 提交转写请求
TranscriptionResult result = transcription.asyncCall(param);
log.info("RequestId: " + result.getRequestId());
// 阻塞等待任务完成并获取结果
result = transcription.wait(
TranscriptionQueryParam.FromTranscriptionParam(param, result.getTaskId()));
// 打印结果
JsonObject output = result.getOutput();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(String.valueOf(output));
JsonNode urlJsonString = root.path("results").get(0).path("transcription_url");
String urlJson = urlJsonString.textValue();
// 1. 发送 HTTP GET 请求获取 JSON
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(urlJson))
.GET()
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
log.info("解析转语音结果请求失败: " + response.statusCode());
}
String jsonContent = response.body();
// 2. 解析 JSON 并提取 text
JsonNode rootText = mapper.readTree(jsonContent);
// 获取 transcripts[0].text
JsonNode transcriptNode = rootText.path("transcripts").get(0);
if (transcriptNode.isMissingNode()) {
log.info("未找到 transcripts 数据");
}
String fullText = transcriptNode.path("text").asText().trim();
log.info("转写文本: " + fullText);
String finalText = fullText.toString().trim();
return finalText;
}
更多推荐
所有评论(0)