import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONObject;
public class VoiceSynthesis {
private static final String serverURL = "http://tsn.baidu.com/text2audio";
private static final String testFileName = "这里改成你的文件路径,可用这种形式e:\\1.mp3";
private static String token = "";
private static final String apiKey = "这里改成你的apiKey";
private static final String secretKey = "这里改成你的secretKey ";
private static final String cuid = "这里是你的唯一标识或IP地址,可用这种形式192.168.1.120";
private static final String lan = "zh";
private static final String tex = "欢迎光临,目前感觉多音字有问题,生当做人杰,当了手机买西瓜,两个当字发音都不对!";//
public static void main(String[] args) throws Exception {
getToken();//获取密钥,可改成对比过期后再获取
method1();//访问百度语音,获取返回的mp3二进制流
}
//获取密钥过程,全局变量存储
private static void getToken() throws Exception {
String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials"
+ "&client_id=" + apiKey + "&client_secret=" + secretKey;
HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL)
.openConnection();
token = new JSONObject(printResponse(conn)).getString("access_token");
}
//返回的数据默认是16K采样率,2字节精度,单声道的PCM数据。可以自己保存到文件进行播放。
private static void method1() throws Exception {
PrintWriter out = null;
BufferedReader in = null;
File pcmFile = new File(testFileName);
try {
HttpURLConnection conn = (HttpURLConnection) new URL(serverURL
+ "?cuid=" + cuid + "&ctp=1&lan="+lan+"&per=0&pit=5&spd=5" +"&tex="+URLEncoder.encode(tex,"UTF-8")+"&tok=" + token).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "audio/mp3;charset=UTF-8");
conn.setDoInput(true);
conn.setDoOutput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// flush输出流的缓冲
out.flush();
InputStream is=conn.getInputStream();
OutputStream output =new FileOutputStream(pcmFile);
//缓冲文件输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
//缓冲文件输出流
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(output);
//解释这个fis.read(bytes)的意思:从读取流"读取数组长度"的数据(打印len可知),并放入数组
int i = 0;
//缓冲区的大小
byte[] buffer = new byte[512];
while(true) {
if(bufferedInputStream.available() < 512) {
while(i != -1) {
i = bufferedInputStream.read();
bufferedOutputStream.write(i);
}
break;
} else {
//当文件的大小还大于512字节时
bufferedInputStream.read(buffer);
bufferedOutputStream.write(buffer);
}
}
//强制清空缓冲区的内容
bufferedOutputStream.flush();
//流的关闭
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
private static String printResponse(HttpURLConnection conn)
throws Exception {
if (conn.getResponseCode() != 200) {
// request error
return "";
}
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(new JSONObject(response.toString()).toString(4));
return response.toString();
}
}
- 1
- 2
前往页