1.增加maven pom.xml 以下jar包:
org.jsoup
jsoup
1.11.3
com.alibaba
fastjson
1.2.68
2.java代码:
package com.test;
import java.io.*;
import java.net.*;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
*小马配音 文字转语音 API http://peiyin.xiaomawenku.com/api.html
*
* HTTP方式調用接口
* 充值地址:http://peiyin.xiaomawenku.com/svip_api.html?svip_type_pay=1
*
*/
public class HttpXiaoMaPeiYin {
public static void main(String[] args) {
getXiaomapeiyin();
}
private static void getXiaomapeiyin() {
try {
String urlStr = "http://peiyin.xiaomawenku.com/PeiYin/xiaomapeiyin_api_to_peiyin";
Map params=new HashMap();
//xiaomapeiyin_svip_user_id
params.put("xiaomapeiyin_svip_user_id", "3339");
//xiaomapeiyin_svip_token客服给你的秘钥
params.put("xiaomapeiyin_svip_token", "xiaomapeiyinapi123d823339be5148e80aca9ea8ad942b13a3faccfbe1c91df0e9b");
//: 配音内容
String xiaomapeiyin_text="1小马配音123";
//内容编码
//xiaomapeiyin_text =URLEncoder.encode(xiaomapeiyin_text, "UTF-8");
params.put("xiaomapeiyin_text", xiaomapeiyin_text);
//: 采样率 不用动
params.put("caiyanglv", "8");
//: 格式后缀 不用动
params.put("geshi", "3");
//: 主播id
params.put("renming_id", "19");
// : 音调
params.put("pitchRate", "0");
//: 语速
params.put("speechRate", "0");
// : 音量
params.put("volume", "50");
//: 背景音乐
params.put("bgurl", "");
// post参数
StringBuilder postData = new StringBuilder();
for (Map.Entry param : params.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = new byte[0];
postDataBytes = postData.toString().getBytes("UTF-8");
//开始访问
HttpURLConnection conn = (HttpURLConnection)(new URL(urlStr)).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setConnectTimeout(99999999);
conn.setReadTimeout(99999999);
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = null;
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;) {
sb.append((char)c);
}
in.close();
conn.disconnect();
String responseStr = sb.toString();
System.out.println(responseStr);
JSONObject outJson = JSONObject.parseObject(responseStr);
//json结果解析 xiaomapeiyin_result_url
JSONObject outJsonq = JSONObject.parseObject(outJson.get("data").toString());
System.out.println(outJsonq.toString());
String xiaomapeiyin_result_url= outJsonq.get("xiaomapeiyin_result_url").toString();
//获得配音音频文件地址
System.out.println(xiaomapeiyin_result_url);
//然后流式下载即可保存mp3
/**
* 下载音频
*/
//开始访问
HttpURLConnection conn2 = (HttpURLConnection)(new URL(xiaomapeiyin_result_url)).openConnection();
conn2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn2.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn2.setConnectTimeout(999999);
conn2.setReadTimeout(999999);
conn2.setDoOutput(true);
conn.connect();
// 写盘 配音文件存到本地磁盘
RandomAccessFile file = new RandomAccessFile("xiaomapeiyin.mp3" , "rw");
InputStream stream = conn2.getInputStream();
byte buffer[] = new byte[1024];
while (true) {
int len = stream.read(buffer);
if (len == -1) {
break;
}
file.write(buffer, 0, len);
}
if (file != null) {
file.close();
}
if (stream != null) {
stream.close();
}
conn2.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|