欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

一步一步打造Java Web机器人(第二部分完结)

最编程 2024-08-09 17:18:04
...
public void process() { SpeechSynthesizer synthesizer = null; try { //创建实例,建立连接。 synthesizer = new SpeechSynthesizer(client, getSynthesizerListener()); synthesizer.setAppKey(appKey); //设置返回音频的编码格式 synthesizer.setFormat(OutputFormatEnum.valueOf(ttsParam.getFormat())); //设置返回音频的采样率 synthesizer.setSampleRate(ttsParam.getSampleRate() == 8000 ? SampleRateEnum.SAMPLE_RATE_8K : SampleRateEnum.SAMPLE_RATE_16K); //发音人 // synthesizer.setVoice("Aixia"); synthesizer.setVoice(ttsParam.getVoice()); //语调,范围是-500~500,可选,默认是0。 synthesizer.setPitchRate(0); //语速,范围是-500~500,默认是0。 synthesizer.setSpeechRate(ttsParam.getSpeechRate()); //设置用于语音合成的文本 synthesizer.setText(ttsParam.getText()); // 是否开启字幕功能(返回相应文本的时间戳),默认不开启,需要注意并非所有发音人都支持该参数。 synthesizer.addCustomedParam("enable_subtitle", false); //此方法将以上参数设置序列化为JSON格式发送给服务端,并等待服务端确认。 long start = System.currentTimeMillis(); synthesizer.start(); log.info("tts start latency " + (System.currentTimeMillis() - start) + " ms"); SpeechSynthesizerUtil.startTime = System.currentTimeMillis(); //等待语音合成结束 synthesizer.waitForComplete(); log.info("tts stop latency " + (System.currentTimeMillis() - start) + " ms"); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 if (null != synthesizer) { synthesizer.close(); } } }

推荐阅读