简介:本文详细介绍百度语音合成与语音识别API的Java版本使用方法,涵盖环境准备、API调用流程、参数配置、错误处理及最佳实践,助力开发者快速集成语音功能。
本文聚焦百度语音合成与语音识别API的Java版本使用,从环境搭建、API调用流程、参数配置、错误处理到最佳实践,提供完整的开发指南。通过代码示例与详细说明,帮助开发者快速掌握语音功能的集成方法,适用于智能客服、语音交互、内容创作等场景。
使用百度语音API前,需完成以下步骤:
API Key和Secret Key。AppID,后续调用需使用。
<!-- Maven依赖示例 --><dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>4.16.11</version></dependency>
api.baidu.com)。
import com.baidu.aip.speech.AipSpeech;public class SpeechSynthesizer {private static final String APP_ID = "你的AppID";private static final String API_KEY = "你的API Key";private static final String SECRET_KEY = "你的Secret Key";public static void main(String[] args) {AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);}}
核心参数说明:
| 参数 | 类型 | 说明 | 示例值 |
|———|———|———|————|
| tex | String | 待合成文本(需URL编码) | “你好,世界” |
| lan | String | 语言类型(zh/en等) | “zh” |
| ctp | String | 合成音色(1=女声,0=男声) | “1” |
| spd | String | 语速(-500~500) | “5” |
| pit | String | 音调(-500~500) | “5” |
| vol | String | 音量(0~15) | “10” |
import com.baidu.aip.speech.TtsResponse;import com.baidu.aip.util.Util;import java.io.FileOutputStream;public class SpeechSynthesizer {public static void main(String[] args) {AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);String text = "百度语音合成API让开发更简单";String filePath = "output.mp3";// 调用合成接口TtsResponse res = client.synthesis(text, "zh", 1, null);// 获取二进制音频数据byte[] data = res.getData();if (data != null) {try (FileOutputStream fos = new FileOutputStream(filePath)) {fos.write(data);System.out.println("音频文件已保存至:" + filePath);} catch (Exception e) {e.printStackTrace();}} else {System.out.println("合成失败:" + res.getErrorCode() + ":" + res.getErrorMsg());}}}
import com.baidu.aip.speech.AipSpeech;public class SpeechRecognizer {private static final String APP_ID = "你的AppID";private static final String API_KEY = "你的API Key";private static final String SECRET_KEY = "你的Secret Key";public static void main(String[] args) {AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);// 可选:设置识别参数client.setConnectionTimeoutInMillis(2000);}}
核心参数说明:
| 参数 | 类型 | 说明 | 示例值 |
|———|———|———|————|
| format | String | 音频格式(wav/pcm/amr等) | “wav” |
| rate | int | 采样率(8000/16000) | 16000 |
| channel | int | 声道数(1/2) | 1 |
| cuid | String | 用户唯一标识(可选) | “user123” |
| len | int | 音频长度(毫秒,实时识别可忽略) | 3000 |
import com.baidu.aip.speech.AsrResponse;import java.io.FileInputStream;public class SpeechRecognizer {public static void main(String[] args) {AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);String filePath = "test.wav";try (FileInputStream fis = new FileInputStream(filePath)) {byte[] data = new byte[fis.available()];fis.read(data);// 调用识别接口AsrResponse res = client.asr(data, "wav", 16000, null);// 解析识别结果String result = res.getResult();System.out.println("识别结果:" + result);} catch (Exception e) {e.printStackTrace();}}}
对于长音频或实时流,推荐使用WebSocket协议:
// 需实现WebSocket客户端,此处为简化示例// 实际开发中可使用Netty或Tyrus等库public class RealTimeRecognizer {public static void main(String[] args) {// 1. 建立WebSocket连接// 2. 发送二进制音频数据// 3. 接收并解析JSON格式的识别结果// 示例结果:// {"corpus_no":"123","err_no":0,"result":["你好"],"sn":"12345"}}}
| 错误码 | 说明 | 解决方案 |
|---|---|---|
| 110 | 认证失败 | 检查API Key/Secret Key是否正确 |
| 111 | 配额不足 | 升级服务套餐或优化调用频率 |
| 112 | 音频格式不支持 | 确认format参数与音频实际格式一致 |
| 113 | 音频过长 | 单次请求音频不超过60秒 |
百度支持多种发音人(如情感合成、方言合成),需在控制台申请权限后使用:
// 示例:使用情感合成发音人TtsResponse res = client.synthesis(text, "zh", 1,new HashMap<String, String>() {{put("per", "4"); // 4=情感合成-温柔女声}});
结合百度语音唤醒API,可实现“小度小度”等唤醒功能,适用于IoT设备开发。
支持中英文混合识别,需设置language参数为mix:
AsrResponse res = client.asr(data, "wav", 16000,new HashMap<String, String>() {{put("language", "mix");}});
本文详细介绍了百度语音合成与识别API的Java版本使用方法,涵盖环境配置、核心功能调用、错误处理及优化建议。开发者可通过以下资源进一步学习:
通过合理使用这些API,开发者可快速为应用添加语音交互能力,提升用户体验与产品竞争力。