当前位置: 首页 > news >正文

百度语音合成API调用

背景

将文本内容合成语音,通过网页扬声器播放出来。

方法

使用Baidu的SDK

添加依赖

<dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>${version}</version>
</dependency>

注意点:

添加SDK工具包aip-java-sdk-version.jar和第三方依赖工具包json-20160810.jar log4j-1.2.17.jar

详细代码:

public static void synthesizeAndSaveQuestions(List<String> questions) throws IOException {// 设置请求参数HashMap<String, Object> options = new HashMap<>();options.put("spd", 5); // 语速,取值0 - 9,默认为5中语速options.put("pit", 5); // 音调,取值0 - 9,默认为5中语调options.put("vol", 9); // 音量,取值0 - 9,默认为5中音量options.put("per", 0); // 发音人选择, 0为度小美,1为度小宇,3为度逍遥,4为度丫丫,默认为度小美String basePath = "D:";for (int i = 0; i < questions.size(); i++) {String question = questions.get(i);String sanitizedQuestion = question.replaceAll("[\\/*?:\"<>|]", "_");String relativePath = "/upload/questionAudio/question_" + (i + 1) + "_" + sanitizedQuestion + ".mp3";String fullPath = basePath + relativePath;TtsResponse res = client.synthesis(question, "zh", 1, options);byte[] data = res.getData();org.json.JSONObject res1 = res.getResult();if (data != null) {File file = new File(fullPath);File parentDir = file.getParentFile();if (!parentDir.exists()) {if (!parentDir.mkdirs()) {throw new IOException("无法创建目录: " + parentDir.getAbsolutePath());}}try (FileOutputStream fos = new FileOutputStream(file)) {fos.write(data);} catch (IOException e) {throw new IOException("音频文件写入失败", e);}}if (res1 != null) {System.out.println(res1.toString(2));}}}public static String recognizeAudioFilesInDirectory(String directoryPath) {StringBuilder combinedResult = new StringBuilder();File audioDir = new File(directoryPath);for (File partFile : audioDir.listFiles()) {if (partFile.isFile() && partFile.getName().endsWith(".wav")) {try {byte[] audioData = Files.readAllBytes(partFile.toPath());org.json.JSONObject res = client.asr(audioData, "wav", 8000, null);if (res.has("result")) {combinedResult.append(res.getJSONArray("result").getString(0));} else {System.out.println("文件 " + partFile.getName() + " 识别失败,错误信息: " + res);}} catch (IOException e) {e.printStackTrace();}}}return combinedResult.toString();}public static String splitAndRecognizeAudio(File audioFile) {System.out.println("audioFile.getAbsolutePath(): " + audioFile.getAbsolutePath());File longAudioFile = audioFile;// 检查文件是否存在if (!longAudioFile.exists()) {System.err.println("文件不存在: " + longAudioFile.getAbsolutePath());return "";}int segmentLength = 60;AudioSplitter.splitAudioFile(longAudioFile, segmentLength);System.out.println("split complete...");String splitAudioDirectory = longAudioFile.getParent();System.out.println("splitAudioDirectory: " + splitAudioDirectory);String recognitionResult = recognizeAudioFilesInDirectory(splitAudioDirectory);System.out.println("拼接后的识别结果: " + recognitionResult);return recognitionResult;}

相关文章:

  • SOLIDWORKS广东东莞地区代理商哪个服务好?都有哪些代理商?
  • 1.1 点云数据获取方式——引言
  • 图漾官网Sample_V1版本C++语言完整参考例子---单相机版本
  • Java练习6
  • 大数据项目全生命周期工具链解析
  • ​MCP协议深度解析:原理、应用与物联网时代的机遇-优雅草卓伊凡
  • 认识Linux基本操作、命令
  • Spring Boot 集成 ActiveMQ 实现异步消息通信(二)
  • 面试篇 - LoRA(Low-Rank Adaptation) 原理
  • 《图像采集与处理技术的研究与洞察》
  • Vue 3 浏览器使用 Composition API
  • 开源模型应用落地-qwen模型小试-Qwen3-8B-快速体验(一)
  • 在 JMeter 中使用 BeanShell 获取 HTTP 请求体中的 JSON 数据
  • 【计算机架构】CISC(复杂指令集计算机)架构
  • 【Science】强耦合手性准BIC驱动动量空间可编程高Q圆偏振激光——哈工大突破拓扑光子学新维度
  • java的多线程
  • [Python]非零基础的快速上手
  • 染色质开放性测序(ATAC-seq)
  • ElasticSearch深入解析(六):集群核心配置
  • MATLAB长方体磁体3D磁力线生成
  • 科学家为AI模型设置“防火墙”,以防止被不法分子滥用
  • 厚重与潮流交织,淮安展现“运河之都”全新城市想象
  • 外交部:美方应在平等、尊重和互惠的基础上同中方开展对话
  • 助力企业高质量出海,上海静安发放服务包、服务券
  • 物业也能成为居家养老“服务员”,上海多区将开展“物业+养老”试点
  • 贸促会答澎湃:5月22日将举办2025年贸易投资促进峰会