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;
}可以参考下这个。此回答整理自钉群"阿里语音AI【6群】"
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。