BodyAsStream();
// filePath是要保存文件的路径(含文件名、PCM文件后缀)
String filePath = "filePath";
FileUtils.copyInputStreamToFile(rpsContent, new File(filePath));
} else {
String errorString = postMethod.getResponseBodyAsString();
System.out.println(errorString);
}
} else {
System.out.println("callApi failed: ret =" + ret + " rsp=" + postMethod.getResponseBodyAsString());
}
}
注意:
上述代码中xxxxx对应的值请根据实际情况填写,具体取值请参见同步接口(流式)。
2.2异步接口
2.2.1创建异步任务
通过access_token信息,发送HTTPS POST创建歌声合成异步任务。
示例代码(Java)如下所示:
其中requestUrl = "https://audioeditor-api-drcn.cloud.huawei.com/v1/audioeditor/gateway/ai/ttsing/async/task/create"。
请点击下载MusicXML文件,并上传:
/**
* 调用创建异步任务接口
* @throws Exception IO异常
*/
private static void creatAsyncTask() throws Exception {
// 设置请求header
PostMethod postMethod = new PostMethod(requestUrl);
// 设置文本类型(String),例:"application/json;charset=utf-8"
postMethod.setRequestHeader("Content-Type", contentType);
// 设置请求ID(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
postMethod.setRequestHeader("X-Request-ID", requestId);
// 设置App包名(String),例:"com.huawei.demo"
postMethod.setRequestHeader("X-Package-Name", pacageName);
// 设置App所在国家(String),例:"cn"
postMethod.setRequestHeader("X-Country-Code", countryCode);
// 设置App标识(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
postMethod.setRequestHeader("HMS-APPLICATION-ID", applicationId);
// 设置证书指纹(String),例:"xxxxxxxxxxxxxxx"
postMethod.setRequestHeader("certFingerprint", certFingerprint);
// 设置动态获取的AccessToken(String)
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// 设置请求body
Map<String, Object> bodyMap = new HashMap<>();
Map<String, Object> dataMap = new HashMap<>();
Map<String, Object> configMap = new HashMap<>();
// filePath是MusicXML文件路径(含文件名、后缀)
String lyricFilePath = "filePath";
dataMap.put("lyric", FileUtils.readFileToString(new File(lyricFilePath), "UTF-8"));
dataMap.put("language", "chinese");
configMap.put("type", 1);
configMap.put("outputEncoderFormat", 0);
configMap.put("wordDurationForceAlign", "false");
bodyMap.put("data", dataMap);
bodyMap.put("config", configMap);
RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int ret = httpClient.executeMethod(postMethod);
String rpsContent = postMethod.getResponseBodyAsString();
if (ret == 200) {
System.out.println(rpsContent);
} else {
System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
}
}
使用预置曲目输入歌词:
/**
* 调用创建异步任务接口
* @throws Exception IO异常
*/
private static void creatAsyncTask() throws Exception {
// 设置请求header
PostMethod postMethod = new PostMethod(requestUrl);
// 设置文本类型(String),例:"application/json;charset=utf-8"
postMethod.setRequest