登录百度智能云账号
https://console.bce.baidu.com/ai/?_=1654758432357&fromai=1#/ai/speech/app/list 访问此链接创建应用
创建完成后点击应用列表,记录API Key 和 Secret Key
使用API Key 和 Secret Key 获取token
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class AuthService {
public static String getAuth() {
String clientId = "NcWgr1WofAsov";
String clientSecret = "wodN6GUcv0NRIBHfHjBA";
return getAuth(clientId, clientSecret);
}
public static String getAuth(String ak, String sk) {
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
+ "grant_type=client_credentials"
+ "&client_id=" + ak
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
使用hutool工具包调用百度长语音识别接口创建语音转换进程
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.19</version>
</dependency>
Map<String,Object> body = new HashMap<>();
body.put("speech_url","这里输入外网可访问的音频地址");
body.put("format","m4a");
body.put("pid","80001");
body.put("rate","16000");
String result = HttpUtil.post("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?access_token=24.09fb9ae129423ec2f242048e4ad45fc6.2592000.1657344328.282335-26404843", JSONObject.valueToString(body));
if (result != null) {
JSONObject resultJson = new JSONObject(result);
System.out.println("resultJson"+resultJson);
}
将返回值中的task_id保存用于获取转换后的文字
String[] key = {"62a185bc72dec60c414b7f4b"};
Map<String,Object> bodys = new HashMap<>();
body.put("task_ids",key);
String results = HttpUtil.post("https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token=24.09fb9ae129423ec2f242048e4ad45fc6.2592000.1657344328.282335-26404843", JSONObject.valueToString(bodys));
if (results != null) {
JSONObject resultJsons = new JSONObject(results);
JSONArray tasks_info = (JSONArray) resultJsons.get("tasks_info");
JSONObject o = (JSONObject) tasks_info.get(0);
JSONObject task_result = (JSONObject) o.get("task_result");
JSONArray detailed_result = (JSONArray) task_result.get("detailed_result");
for (int i = 0; i < detailed_result.length(); i ++){
JSONObject o1 = (JSONObject) detailed_result.get(i);
String res = o1.get("res").toString();
System.out.println(res);
}
}