Google言论的空白回复

时间:2018-01-31 09:57:02

标签: android rest speech-recognition google-cloud-speech

  

我正在使用Google Speech REST API将语音转换为文本   得到空白的回应。这是我在Http中传递的json   邮寄申请: -

这是我获取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();
    }

1 个答案:

答案 0 :(得分:1)

您是否检查了official docs中的问题排查?在那里说

  

"如果没有返回成绩单(例如,您收到一个空的{} JSON   响应)并且没有发生错误,音频很可能是   没有使用正确的编码。"

您应该在encode your file之前确保音频编码与JSON文件的参数匹配。在您的情况下"encoding": "FLAC""sampleRateHertz":16000