Search code examples
google-cloud-speech

Google audio to text API but return null


I have send below JSON request but I am getting below error.

JSON Request:-

{
  "config": {
    "enableAutomaticPunctuation": "true",
    "encoding": "MULAW",
    "languageCode": "en-US",
    "model": "default",
    "sampleRateHertz": 8000
  },
  "audio": {
    "content": "QzpcU3BlZWNoVG9UZXh0XGVuZ2xpc2hcUENNXDIud2F2"
  }
}

Output: null

Method for Encoding of wav file as given below

byte[] encodedAudio = Base64.encodeBase64(pcmFilePath.getBytes());
String s = new String(encodedAudio);

Solution

  • If pcmFilePath is a String, then pcmFilePath.getBytes() will return the path to the file. Therefore, encodedAudio will contain the path to the file, not the encoded audio.

    One way to get the contents of the file would be to use java.nio.file.Files.readAllBytes() introduced in JDK 7.

    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    byte[] pcmBytes = Files.readAllBytes(Paths.get(pcmFilePath));
    byte[] encodedAudio = Base64.encodeBase64(pcmBytes);