Google 音频转文本 API 但 return 为空

Google audio to text API but return null

我已发送以下 JSON 请求,但出现以下错误。

JSON 要求:-

{
  "config": {
    "enableAutomaticPunctuation": "true",
    "encoding": "MULAW",
    "languageCode": "en-US",
    "model": "default",
    "sampleRateHertz": 8000
  },
  "audio": {
    "content": "QzpcU3BlZWNoVG9UZXh0XGVuZ2xpc2hcUENNXDIud2F2"
  }
}

输出:空

wav文件的编码方法如下

byte[] encodedAudio = Base64.encodeBase64(pcmFilePath.getBytes());
String s = new String(encodedAudio);

如果 pcmFilePathString,则 pcmFilePath.getBytes() 将 return 文件路径。因此,encodedAudio 将包含文件的路径,而不是编码后的音频。

获取文件内容的一种方法是使用 JDK 7.

中介绍的 java.nio.file.Files.readAllBytes()
import java.nio.file.Files;
import java.nio.file.Paths;

byte[] pcmBytes = Files.readAllBytes(Paths.get(pcmFilePath));
byte[] encodedAudio = Base64.encodeBase64(pcmBytes);