百度云文档ttps://ai.baidu.com/ai-doc/SPEECH/Bk5difx01
依赖:
org.jsonjson20210307
示例代码:
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import lombok.extern.slf4j.Slf4j;import okhttp3.*;import org.json.JSONObject;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.concurrent.TimeUnit;/** * 语音识别 */@Slf4j@Componentpublic class AasrUtils {public static final String API_KEY = "U92RRV****ag9xZv";public static final String SECRET_KEY = "SU05xD****0ziDkM";static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS) // Set the connection timeout.readTimeout(300, TimeUnit.SECONDS)// Set the read timeout.build();/** * 音频转写 * * @param speechUrl音频url, 音频大小不超过500MB * @return * @throws IOException */public static String createAasr(String speechUrl) throws IOException {MediaType mediaType = MediaType.parse("application/json");RequestBody body = RequestBody.create(mediaType, "{\n" +"\"speech_url\": \"" + speechUrl + "\",\n" +"\"format\": \"mp3\",\n" +"\"pid\": 80001,\n" +"\"rate\": 16000\n" +"}");Request request = new Request.Builder().url("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?access_token=" + getAccessToken()).post(body).addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build();try (Response response = HTTP_CLIENT.newCall(request).execute()) {if (response.isSuccessful()) {String responseBody = response.body().string();log.info("创建AACR任务响应: {}", responseBody);String taskId = JSON.parseObject(responseBody).getString("task_id");return taskId;} else {log.info("创建AACR任务失败: {}", response.code());return null;}}}public static String checkASRStatus(String taskId) throws IOException {if (StringUtils.isEmpty(taskId)) {return "";}String taskStatus = "";while (!("Success".equals(taskStatus) || "Failed".equals(taskStatus))) {// Add a delay before the next querytry {Thread.sleep(1000); // Sleep for 1 seconds (adjust as needed)} catch (InterruptedException e) {e.printStackTrace();}HashMap map = new HashMap();ArrayList