Question

Can I send requests to google speech api from my java swing program as from Android? I mean how to use android libraries in simple java program. For example, just one JFrame, jpanel, two buttons: "start", "stop", and text field. If yes, tell from what I have to begin.b

Was it helpful?

Solution

The answer is no, you cannot use Android speech recognition libraries from your desktop Java Swing program.

Instead, you could use the Google speech recognition web service, but please note this is not officially supported and it could change or be removed without previous notice (but not very likely, because it is what the Chrome browser uses to provide speech recognition capabilities).

To use it from Java, you will need to POST a HTTP request with a FLAC encoded audio to "https://www.google.com/speech-api/v1/recognize".

The relevant data to build the request (with sample values):

  • HTTP Method: POST
  • URL: "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=10&pfilter=0"
  • xjerr tells the speech server to return the errors as part of the JSON, not as HTTP error codes.
  • client AFAIK it is not really taken into account, but if you say "chromium" you are on the safe side because it seems the requests are sent from Chromium
  • lang language of the speech
  • maxresults max number of hypotheses being returned
  • pfilter to turn the "safe" filter on/off
  • "Content-Type:audio/x-flac; rate=16000" Replace 16000 by the frequency (in Hz) of your FLAC file.

With this you will receive a JSON with the different hypotheses (alternatives) that the speech engine has found.

To encode a file in FLAC format from Java you can use a library such as jFLAC (http://jflac.sourceforge.net/).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top