官网地址:https://ai.baidu.com/ai-doc/SPEECH/mlbxh7xie
第一步:自行准备好 APPID、API Key、Secret Key
第二部:导入依赖
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.8.0</version>
</dependency>
最后
private static final String APP_ID = "***************";
private static final String API_KEY = "***************";
private static final String SECRET_KEY = "***************";
public void convertVoice(){
// 初始化一个AipSpeech
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 调用接口
TtsResponse res = client.synthesis("风萧萧兮易水寒", "zh", 1, null);
byte[] data = res.getData();
org.json.JSONObject res1 = res.getResult();
if (data != null) {
try {
// 将文件保存到指定目录下的唯一文件名
String savePath = "D:\\voice\\";
File directory = new File(savePath);
if (!directory.exists()) {
directory.mkdirs(); // 如果目录不存在,创建目录
}
String uniqueFileName = UUID.randomUUID().toString() + ".mp3";
String filePath = savePath + uniqueFileName;
Util.writeBytesToFileSystem(data, filePath);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (res1 != null) {
System.out.println(res1.toString(2));
}
}