Search code examples
androidgoogle-speech-apigoogle-apis-explorer

Base64 decoding failed for Google Speech API


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 ?


Solution

  • 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