gpt4 book ai didi

Java 语音 API 空响应

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:51 28 4
gpt4 key购买 nike

我正在使用 Java 语音识别 API - Jarvis,位于 https://github.com/lkuza2/java-speech-api

但是,当我运行应用程序时,出现错误:服务器返回 HTTP 响应代码:400 对于 URL:https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=1 (这是此 api 用于获取 Google 响应的 URL)

我还创建了一个 API key ,如之前的帖子中提到的,并尝试使用网址(这是版本 2 API):www.google.com/speech-api/v2/recognize?output=json&lang=en-US&key=MYKey"。但在这种情况下,我从 Google 得到了 null 响应。

有人可以告诉我如何解决这个问题吗?

最佳答案

我从 Recognizer 类中更改了一些内容:

我将 GOOGLE_RECOGNIZER_URL 常量更改为:

    private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_KEY";

然后我改变了这个方法,因为响应数据有2行

   private String rawRequest(File inputFile, int maxResults, int sampleRate) throws IOException

第一行(读取和发送的行)为空(我真的不知道为什么),第二行有识别的语音响应。为此你必须阅读第二行(不知道是否有更好的方法):

    String response = br.readLine();
response = br.readLine();
br.close();
return response;

然后我更改此方法,我认为它使用的是 v1 URL 响应或其他内容,因为此方法在 json 响应中查找话语,但没有话语键。

    private void parseResponse(String rawResponse, GoogleResponse googleResponse)
if (rawResponse == null)
return;

JSONObject jsonObject = new JSONObject(rawResponse);
JSONArray jsonArray= (JSONArray) jsonObject.get("result");
JSONArray jsonArrayResult = (JSONArray) jsonArray.getJSONObject(0).get("alternative");
googleResponse.setResponse(jsonArrayResult.getJSONObject(0).get("transcript").toString());
googleResponse.setConfidence(jsonArrayResult.getJSONObject(0).get("confidence").toString());

我是 json 库的新手,所以它可能是一种更好、更短的方法,但这对我有用!

关于Java 语音 API 空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206749/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com