2
votes

I tried to send a POST request to https://speech.googleapis.com/v1/speech:recognize using the JSON and the code fragment below. Somehow google responsed that fail to decoding Base 64 in my request.

{ "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);

"code": 400, "message": "Invalid value at 'audio.content' (TYPE_BYTES), Base64 decoding failed for \"...

Does anyone have any suggestion ?

1
Essentially, what it says in the message. ZXCVBNM is invalid (incomplete) base64-encoding (it partially decodes to epò but is incomplete). Base-64 encodes every three characters of source (binary) data into 4 characters of encoded data. You only have seven characters so is incomplete (and also suspiciously short for any type of audio representation). - TripeHound
I realised later that you're replacing ZXCVBNM with what is meant to be your file content (I don't know Android enough to know that your code is reading the file correctly or not). However, the point remains that what Google is receiving isn't valid base-64. Try logging what you're actually sending in the payload to see if it makes sense. (Or, conceivably, Googole isn't expecting base64 (the link is giving 404 for me). - TripeHound
Sorry for the late reply. I send the POST request following this document: cloud.google.com/speech/reference/rest/v1/speech/recognize. May be the link you are trying to access isn't made for browser. Here is the logging I actually sending in the pay load mediafire.com/file/9qzxyzexlxc7jr1 - ZodiacLeo123

1 Answers

5
votes

I managed to get the result from Google Speech API.

It was documented that the Base 64 encoding should not have line-wrapping Link: https://cloud.google.com/speech/docs/base64-encoding

Changing from Base64.DEFAULT to Base64.NO_WRAP worked in my case. Also the pcm file should be LSB