谷歌语音 API V1无法工作?

11

我使用了Google语音API v1开发了一个应用程序。

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

这个链接被用来获取响应。它以前工作得很好,但是从今天开始它不再起作用了。我没有从那个链接中得到任何响应。有人有什么思路吗?是否有任何替代链接?请帮帮忙。

  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;
}

有没有其他人遇到同样的问题?


3
没有回应,也没有错误。 - Raptor
java.io.FileNotFoundException: https://www.google.com/speech-api/v1 这是我得到的,响应为空。 - Sunil Sunny
它昨天还能工作。我会发布我的代码,请查看编辑。 - Sunil Sunny
2
是的,谷歌开始封锁大量使用API的用户,请考虑切换到其他服务。 - Nikolay Shmyrev
有没有其他的方法呢? - Sunil Sunny
显示剩余4条评论
3个回答

14

2
如果您正在寻找Java API,请查看这个。它支持新的Duplex API和V2 API。还附带有一个方便的教程。如果谷歌尚未关闭V1,建议在谷歌彻底关闭之前进行迁移。

0

Chrome的语音API无法获得额外的配额。请查看Cloud Speech API

不要在任何Chromium组/邮件列表中发布有关语音API的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接