我使用的服务是:
调用的接口是:
参数设置情况:
开发平台和机型:
SDK版本号:
代码或日志截图(上传截图能帮助您更快解决问题):
复现步骤、现象及其他描述:
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
xahef:
回复xahef:贴一下 String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
"&client_id=" + apiKey + "&client_secret=" + secretKey; 这段的结果
//put your own params here
private String apiKey = "";
private String secretKey = "";
这个填了么?
回复fujiayi1984:在控制台中无法获取那段结果
您好,能贴下 serverURL
+ "?tex=" + new String((text).getBytes(),"UTF-8") + "&lan=zh" + "&cuid=" + cuid + "&ctp=1" + "&tok=" + token
+"&per=" + per + "&spd=" + spd + "&pit=" + pit+ "&vol=" + vol 这段的结果么?您可以放到浏览器中试试
xie_yongkang:
您好, 请附上您的代码, 并注意屏蔽个人的AK/SK信息, 以便排查, 谢谢。
回复xie_yongkang:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
private String token = "";
private String text = "";
private String saveURL = "";
private boolean isDetailed = false;
//pit6最好啦
private int per = 0; //男1女0
private int spd = 6; //音速0-9
private int pit = 6; //音调0-9
private int vol = 5; //音量0-9
//put your own params here
private String apiKey = "";
private String secretKey = "";
private String cuid = "";
public SpeechSynthesis(String cuid){
this.cuid = cuid;
checkLocalToken();
}
public SpeechSynthesis(String apiKey , String secretKey , String cuid){
this.apiKey = apiKey;
this.secretKey = secretKey;
this.cuid = cuid;
checkLocalToken();
}
private void checkLocalToken() {
// TODO Auto-generated method stub
File tokenFile = new File("TTS_token.dat");
if(tokenFile.exists()){
try {
token = new Scanner(tokenFile).nextLine();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
try {
getToken();
PrintWriter output = new PrintWriter(tokenFile);
output.print(token);
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("获取认证失败,请检查apiKey,secreKey,cuid位置或者内容是否设置正确");
System.exit(1);
}
}
}
//与验证服务器验证 获取token
public 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");//获取access_token:...后的taken
}
private String printResponse(HttpURLConnection conn) throws Exception {
if (conn.getResponseCode() != 200) {
// request error
System.err.println("请求失败,请检查网络");
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();
if(isDetailed){
System.out.println( new JSONObject(response.toString()).toString(4));
}
return response.toString();
}
//把语音文件转换成byte数组存储
private byte[] getData(HttpURLConnection conn) throws IOException {
InputStream is = conn.getInputStream();
ArrayList byteList = new ArrayList<>();
int value;
while((value = is.read()) != -1){ //读取流的技巧
byteList.add(value);
}
//这里是一个冗余的方式
byte[] bytes = new byte[byteList.size()];
int[] intbytes = new int[bytes.length];
for(int i = 0; i < bytes.length;i++){
intbytes[i] = (int) byteList.get(i);
}
for(int i = 0; i < bytes.length;i++){
bytes[i] =(byte) intbytes[i];
}
System.out.println("语音成功合成");
return bytes;
}
//利用获得的token与语音服务器连接
public void methodByGET() throws Exception {
if(saveURL.isEmpty() || text.isEmpty()){
System.err.println("未识别语音保存位置 或者未设置发送内容");
System.exit(1);
}
HttpURLConnection conn = (HttpURLConnection) new URL(serverURL
+ "?tex=" + new String((text).getBytes(),"UTF-8") + "&lan=zh" + "&cuid=" + cuid + "&ctp=1" + "&tok=" + token
+"&per=" + per + "&spd=" + spd + "&pit=" + pit+ "&vol=" + vol).openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "audio/mp3");
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(new FileOutputStream(saveURL)); //能保存为音频格式说明传递的就是音频本体 而不是html
out.write(getData(conn));
}
public void methodByGET(String text){
setText(text);
try {
methodByGET();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setSpeed(speed);
setPitch(pitch);
setVolume(volume);
}
public void setSpeed(int speed){
if(speed > -1 && speed < 10)
this.spd = speed;
else {
System.err.println("大小范围0-9");
System.exit(1);
}
}
public void setPitch(int pitch){
if(pitch > -1 && pitch < 10)
this.pit = pitch;
else {
System.err.println("大小范围0-9");
System.exit(1);
}
}
public void setVolume(int volume){
if(volume > -1 && volume < 10 )
this.vol = volume;
else{
System.err.println("大小范围0-9");
System.exit(1);
}
}
public void changeSex(){
if(per == 0){
per = 1;
}else{
per = 0;
}
}
public void setIsDetailed(boolean isDetailed){
this.isDetailed = isDetailed;
}
public void setSaveURL(String path){
this.saveURL = path;
}
public void setText(String text){
StringBuffer textBuffer = new StringBuffer(text);
for(int i = 0 ; i < textBuffer.length();i++){
if(textBuffer.charAt(i) == ' '){
textBuffer.replace(i, i+1, ",");
}
}
this.text = textBuffer.toString();
}
public void setToken(String token){
this.token = token;
}
}
您好, 请附上您的代码, 并注意屏蔽个人的AK/SK信息, 以便排查, 谢谢。