Base64 decoding failed for Google Speech API

痴心易碎 提交于 2019-12-13 16:32:23

问题


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:


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



来源:https://stackoverflow.com/questions/43606167/base64-decoding-failed-for-google-speech-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!