如何使用代号一的Google语音API?

How to use Google Speech API from Codename One?

我想从 phone 录制音频,然后将其发送到 google 语音非流 API。我可以使用 Capture.captureAudio() 进行录音,但是我不知道音频编码和采样率是什么,因为它们是必需的 for the api request。 我如何获得音频编码和采样率,以便我可以将它们与我的 API 请求一起发送?

如果您在 Android 上检查 sources,它会记录在 AMR-WB

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(temp.getAbsolutePath());

Google 语音 API 接受 AMR-WB 如果你正确 set audio format.

另一个问题是文件在 3GPP 容器中记录为 AMR-WB,因此您需要一个自定义代码来从 3GPP 中提取音频数据,您可以找到它 here:

// #!AMR\n
private static byte[] AMR_MAGIC_HEADER = {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a};


public byte[] convert3gpDataToAmr(byte[] data) {
    if (data == null) {
        return null;
    }

    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    // read FileTypeHeader
    FileTypeBox ftypHeader = new FileTypeBox(bis);
    // You can check if it is correct here
    // read MediaDataHeader
    MediaDataBox mdatHeader = new MediaDataBox(bis);
    // You can check if it is correct here
    int rawAmrDataLength = mdatHeader.getDataLength();
    int fullAmrDataLength = AMR_MAGIC_HEADER.length + rawAmrDataLength;
    byte[] amrData = new byte[fullAmrDataLength];
    System.arraycopy(AMR_MAGIC_HEADER, 0, amrData, 0, AMR_MAGIC_HEADER.length);
    bis.read(amrData, AMR_MAGIC_HEADER.length, rawAmrDataLength);
    return amrData;
}

另请注意,AMR-WB 的准确性略低,因此您可能需要考虑更详细的 API 原始音频捕获,而不是 codenameone。