Google Speech API 的 Base64 解码失败

Zod*_*123 2 android google-apis-explorer google-speech-api

我尝试使用 JSON 和下面的代码片段向https://speech.googleapis.com/v1/speech:recognize发送 POST 请求。不知何故,谷歌回应说无法在我的请求中解码 Base 64。

{ "config": { "encoding": "LINEAR16", "sampleRateHertz": 16000, "languageCode": "ja-JP", "maxAlternatives": 5, "profanityFilter": false }, "audio": { "content ": "ZXCVBNM" }, }

    String pcmFilePath = "/storage/emulated/0/Download/voice8K16bitmono.pcm";
    File rawFile = new File(pcmFilePath);
    byte[] rawData = new byte[(int) rawFile.length()];
    DataInputStream input = null;
    try {
        input = new DataInputStream(new FileInputStream(rawFile));
        int readResult = input.read(rawData);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (input != null) {
        input.close();
    };

    String base64 = Base64.encodeToString(rawData, Base64.DEFAULT);
    String completePostBody = postBody.replace("ZXCVBNM" , base64);
Run Code Online (Sandbox Code Playgroud)

"code": 400, "message": "'audio.content' (TYPE_BYTES) 处的值无效,\"的 Base64 解码失败...

有人有什么建议吗?

Zod*_*123 5

我设法从 Google Speech API 获得了结果。

据记载,Base 64 编码不应有换行链接:https : //cloud.google.com/speech/docs/base64-encoding

在我的情况下,从Base64.DEFAULT改为Base64.NO_WRAP工作。pcm 文件也应该是 LSB

  • 这是有道理的(一旦您意识到这一点),尽管您链接的文档并没有明确要求(它仅在 Linux 示例中真正提到)。我怀疑使用 `encodeToString()`(这似乎是一件合理的事情)而不是 `byte[]`,就像他们在 Java 示例中所做的那样是问题的一部分——它的默认值显然是换行,大概用于包含在电子邮件等中。 (2认同)