-
登录百度智能云账号
-
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() {
// 官网获取的 API Key 更新为你注册的
String clientId = "NcWgr1WofAsov";
// 官网获取的 Secret Key 更新为你注册的
String clientSecret = "wodN6GUcv0NRIBHfHjBA";
return getAuth(clientId, clientSecret);
}
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ak
// 3. 官网获取的 Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
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工具包调用百度长语音识别接口创建语音转换进程
cn.hutool hutool-all 5.7.19
Mapbody = new HashMap<>(); body.put("speech_url","这里输入外网可访问的音频地址"); body.put("format","m4a");//音频格式支持["mp3", "wav", "pcm","m4a","amr"]单声道,编码 16bits 位深 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));//需将获取到的token拼接到此处 if (result != null) { JSONObject resultJson = new JSONObject(result); System.out.println("resultJson"+resultJson); }
- 将返回值中的task_id保存用于获取转换后的文字
//此处填写task_id多个视频需用逗号分割
String[] key = {"62a185bc72dec60c414b7f4b"};
Map 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);
}
}
总结
以上代码是自己写的demo只提供思路如有错误望告知 


