开发者社区> 问答> 正文

在阿里语音AI中,有没有完整的案例呢?

在阿里语音AI中,有没有完整的案例呢?private static FlowingSpeechSynthesizerListener getSynthesizerListener() {
FlowingSpeechSynthesizerListener listener = null;
try {
listener = new FlowingSpeechSynthesizerListener() {
File f=new File("flowingTts.wav");
FileOutputStream fout = new FileOutputStream(f);
private boolean firstRecvBinary = true;

//流入语音合成开始
@Override
public void onSynthesisStart(FlowingSpeechSynthesizerResponse response) {
System.out.println("name: " + response.getName() +
", status: " + response.getStatus());
}
//服务端检测到了一句话的开始
@Override
public void onSentenceBegin(FlowingSpeechSynthesizerResponse response) {
System.out.println("name: " + response.getName() +
", status: " + response.getStatus());
System.out.println("Sentence Begin");
}
//服务端检测到了一句话的结束,获得这句话的起止位置和所有时间戳
@Override
public void onSentenceEnd(FlowingSpeechSynthesizerResponse response) {
System.out.println("name: " + response.getName() +
", status: " + response.getStatus() + ", subtitles: " + response.getObject("subtitles"));

}
//流入语音合成结束
@Override
public void onSynthesisComplete(FlowingSpeechSynthesizerResponse response) {
// 调用onSynthesisComplete时,表示所有TTS数据已经接收完成,所有文本都已经合成音频并返回。
System.out.println("name: " + response.getName() + ", status: " + response.getStatus()+", output file :"+f.getAbsolutePath());
}
//收到语音合成的语音二进制数据
@Override
public void onAudioData(ByteBuffer message) {
try {
if(firstRecvBinary) {
// 此处计算首包语音流的延迟,收到第一包语音流时,即可以进行语音播放,以提升响应速度(特别是实时交互场景下)。
firstRecvBinary = false;
long now = System.currentTimeMillis();
System.out.println("tts first latency : " + (now - FlowingSpeechSynthesizerDemo.startTime) + " ms");
}
byte[] bytesArray = new byte[message.remaining()];
message.get(bytesArray, 0, bytesArray.length);
System.out.println("write array:" + bytesArray.length);
fout.write(bytesArray);
} catch (IOException e) {
e.printStackTrace();
}
}
//收到语音合成的增量音频时间戳
@Override
public void onSentenceSynthesis(FlowingSpeechSynthesizerResponse response) {
System.out.println("name: " + response.getName() +
", status: " + response.getStatus() + ", subtitles: " + response.getObject("subtitles"));
}
@Override
public void onFail(FlowingSpeechSynthesizerResponse response){
// task_id是调用方和服务端通信的唯一标识,当遇到问题时,需要提供此task_id以便排查。
System.out.println(
"session_id: " + getFlowingSpeechSynthesizer().getCurrentSessionId() +
", task_id: " + response.getTaskId() +
//状态码
", status: " + response.getStatus() +
//错误信息
", status_text: " + response.getStatusText());
}
};
} catch (Exception e) {
e.printStackTrace();
}
return listener;
}

展开
收起
我睡觉不困 2024-08-26 13:25:10 29 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Lindorm AI 能力介绍 立即下载
2023云栖大会:PolarDB for AI 立即下载
2023云栖大会:Lindorm一站式AI数据平台实战 立即下载