开发者社区> 问答> 正文

在阿里语音AI中,现在是这样写的怎么改?

在阿里语音AI中,现在是这样写的怎么改?1c300cbfac23ee84711fc7230611ae83.png
088959cf86246e807287ce2507f3e054.png

展开
收起
我睡觉不困 2024-08-26 13:25:10 36 0
1 条回答
写回答
取消 提交回答
  • 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群】"

    2024-08-26 14:55:00
    赞同 2 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
释放算力潜能加速应用构建Serverless为AI创新提速 立即下载
智算时代,基于 Kubernetes 落地云原生 AI 立即下载
AI让数据库的路走的“更快更远” 立即下载