声音合成 java_【百度语音合成】JavaSDK方式合成语音示例

本文档提供了一个简单的Java程序示例,演示如何使用百度语音合成SDK进行文字到语音的转换。首先从指定链接下载SDK,然后将其导入项目并初始化AipSpeech对象。接着调用synthesis方法,设置速度、音调等参数,将合成的语音保存为MP3文件。整个过程简洁明了。
摘要由CSDN通过智能技术生成

百度语音合成JavaSDK调用还是很简单的哦

第一步下载语音合成的SDK

http://ai.baidu.com/download?sdkId=1

第二步导入项目SDK,并初始化一个AipSpeech对象

public class Test{

public static void main(String[] args) throws IOException {

AipSpeech aipSpeech = new AipSpeech("APPID","APIKEY", "SERCETKEY");

}

第三步 调用合成的方法,并设置一些参数进行文字合成语音

public class Test{

public static void main(String[] args) throws IOException {

AipSpeech aipSpeech = new AipSpeech("APPID", "APIKEY", "SERCETKEY");

HashMap options = new HashMap();

options.put("spd", "5");

options.put("pit", "5");

options.put("per", "4");

TtsResponse ttsResponse = aipSpeech.synthesis("你好小帅", "zh", 1, options);

byte[] aa = ttsResponse.getData();

getFile(aa, "D:/", "1.mp3");

System.out.println(JSON.toJSONString(ttsResponse));

}

第四步 合成的二进制数据存入文件中。示例代码:

public static void getFile(byte[] bfile, String filePath, String fileName) {

BufferedOutputStream bos = null;

FileOutputStream fos = null;

File file = null;

try {

File dir = new File(filePath);

if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在

dir.mkdirs();

}

file = new File(filePath + "\\" + fileName);

fos = new FileOutputStream(file);

bos = new BufferedOutputStream(fos);

bos.write(bfile);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bos != null) {

try {

bos.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

语音合成就完成了一个示例Demo。很简单吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值