百度语音合成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。很简单吧

Logo

一站式 AI 云服务平台

更多推荐