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 解码失败...
有人有什么建议吗?
我设法从 Google Speech API 获得了结果。
据记载,Base 64 编码不应有换行链接:https : //cloud.google.com/speech/docs/base64-encoding
在我的情况下,从Base64.DEFAULT改为Base64.NO_WRAP工作。pcm 文件也应该是 LSB