首页 文章

Google Speech API的Base64解码失败

提问于
浏览
2

我尝试使用JSON和下面的代码片段向https://speech.googleapis.com/v1/speech:recognize发送POST请求 . 不知何故谷歌回应说我的请求无法解码Base 64 .

{“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”:“'audio.content'(TYPE_BYTES)的值无效,Base64解码失败为\”...

有没有人有任何建议?

1 回答

  • 3

    我设法从Google Speech API获得了结果 .

    据记载,Base 64编码不应该有换行链接:https://cloud.google.com/speech/docs/base64-encoding

    Base64.DEFAULT 改为 Base64.NO_WRAP 在我的案例中起作用 . pcm文件也应该是LSB

相关问题