Google speech Api v1 not working?

13,957

Solution 1

Recently Google closed v1 API. V2 API now requires a key and streaming v2 API is limited to 50 requests per day. You can get a key as described here:

http://www.chromium.org/developers/how-tos/api-keys

However there are no guarantees on unlimited usage.

https://github.com/gillesdemey/google-speech-v2

Solution 2

If you are looking for a Java API check this one out. It supports the new Duplex API and V2 API. Comes with a handy tutorial as well. Might be a good idea to migrate before Google shuts down V1 for good if they haven't already.

Share:
13,957
Sunil Sunny
Author by

Sunil Sunny

The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart.

Updated on June 04, 2022

Comments

  • Sunil Sunny
    Sunil Sunny almost 2 years

    I have developed an app using the Google speech Api v1

    https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang="+language_code;

    and this link was used to get the response. It was working fine but just from today it's not working. I am not getting any response from that link. Any one have any idea? Is there any alternative links? Please help

      protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
    
        String urlString = "https://www.google.com/speech-api/v2/recognize?xjerr=1&client=chromium&lang="
                + language_code;
    //      String urlString = "https://www.google.com/speech-api/v2/recognize?    output=json&lang="+language_code+"s&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4";
    
    //      Log.e("SpeechRecognizer  url : ", urlString);
    //       String urlString =
    //       "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=speech2text&lang="
    //       + language_code;
        URL url;
        try {
            Log.e("", "started speech to text");
    
            FLAC_FileEncoder fileEncoder = new FLAC_FileEncoder();
    
            File inputfile = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/SpeechLibrary/speech.wav");
            File outputfile = new File(Environment
                    .getExternalStorageDirectory().getPath()
                    + "/SpeechLibrary/speech.flac");
    
            fileEncoder.encode(inputfile, outputfile);
    
            url = new URL(urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDefaultUseCaches(true);
            con.setConnectTimeout(20000);
    
            con.setReadTimeout(60000);
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setInstanceFollowRedirects(true);
            con.setRequestMethod("POST");
            // con.setRequestProperty("User-Agent",
            // "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
            con.setRequestProperty("Content-Type", "audio/x-flac;rate=16000");
            File file = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/SpeechLibrary/speech.flac");
            byte[] buffer = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(buffer);
            fis.close();
            OutputStream os = con.getOutputStream();
            os.write(buffer);
            os.close();
    
            con.connect();
            con.getResponseMessage();
               InputStreamReader inputStreamReader = new InputStreamReader(
                    con.getInputStream(), "UTF-8");
            BufferedReader br = new BufferedReader(inputStreamReader);
            String s;
            StringBuilder resultContent = new StringBuilder();
    
            while ((s = br.readLine()) != null) {
                resultContent.append(s + "\n");
            }
            JSONObject jsonResponse = new JSONObject(resultContent.toString());
            JSONArray jsonArray = jsonResponse.getJSONArray("hypotheses");
            Log.e("Response String: ", resultContent.toString());
            if (jsonArray.length() != 0) {
    
                output = jsonArray.getJSONObject(0).getString("utterance");
                confidence_level = Double.parseDouble(jsonArray
                        .getJSONObject(0).getString("confidence"));
            } else if (jsonArray.length() == 0) {
    
            }
            // output=resultContent.toString();
    
        } catch (Exception e) {
            output = "";
            confidence_level = -1;
            e.printStackTrace();
    
            Log.e("ERROR IN PARSING:", e.toString());
            if (e.toString().contains("java.net.UnknownHostException:")||e.toString().contains("java.net.SocketException")) {
    
                return "NETWORK ISSUE";
            }
    
        }
        return null;
    }
    

    Is anyone else having the same problem?.