I am using Google Speech REST API to convert speech to text, I am getting blank response. Here is my json which I was passing in Http Post Request:-
This is my code to get JSON:
File file = new File(mOutputFile.getAbsolutePath());
byte[] bytes = new byte[0];
try {
bytes = loadFile(file);
} catch (IOException e) {
e.printStackTrace();
}
byte[] encoded = Base64.encodeBase64(bytes);
String encodedString = new String(encoded);
JSONObject config = new JSONObject();
config.put("encoding", "FLAC");
config.put("sampleRateHertz", 16000);
config.put("languageCode", "en-US");
config.put("enableWordTimeOffsets", false);
JSONObject audio = new JSONObject();
audio.put("content", "" + encodedString);
JSONObject jsonObject = new JSONObject();
jsonObject.put("config", config);
jsonObject.put("audio", audio);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("https://speech.googleapis.com/v1/speech:recognize?key=GOOGLE_API_KEY");
post.setHeader("content-type", "application/json; charset=UTF-8");
StringEntity entity = new StringEntity(jsonObject.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
s = EntityUtils.toString(resp.getEntity());
Log.e("ExecuteTask Response", "--------------" + s);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception exception) {
exception.printStackTrace();
}
Have you checked the troubleshooting in the official docs ? There it says
"If a transcript is not returned (e.g. you receive an empty {} JSON response) and no errors have occurred, it's likely that the audio is not using the proper encoding."
You should make sure before you encode your file that your audio encoding matches the parameters of your JSON file. In your case "encoding": "FLAC" and "sampleRateHertz":16000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With