后台管理应用页面点下载jar包
1.aip-java-sdk-4.1.1.jar
2.json-20160810.jar
3.log4j-1.2.17.jar
package afeng.util;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
public class ToVoice {
//设置APPID/AK/SK
public static final String APP_ID = "你的 App ID";
public static final String API_KEY = "你的 Api Key";
public static final String SECRET_KEY = "你的 Secret Key";
public static void main(String[] args) {
// 初始化一个AipSpeech
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
// client.setConnectionTimeoutInMillis(2000);
// client.setSocketTimeoutInMillis(60000);
// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
// client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("spd", "5");
options.put("pit", "5");
//发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
options.put("per", "4");
String string = "sssssssssss";
// 调用接口
TtsResponse res = client.synthesis(string, "zh", 1, options);
// TtsResponse res = client.synthesis("醉经好房人hello", "zh", 1, null);
byte[] data = res.getData();//语音文件字节
JSONObject res1 = res.getResult();
if (data != null) {
try {
Util.writeBytesToFileSystem(data, "D:/work.mp3");
// 调用接口
// JSONObject res2 = client.asr(data, "pcm", 16000, null);
// System.out.println(res2.toString(2));
} catch (IOException e) {
e.printStackTrace();
}
}
if (res1 != null) {
try {
System.out.println(res1.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}