android - 谷歌语音 Api v1 不工作?

标签 android google-api speech-recognition speech-to-text android-speech-api

我使用 Google speech Api v1 开发了一个应用

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

并且此链接用于获取响应。它工作正常,但从今天开始它不工作了。我没有从那个链接得到任何回应。有人知道吗?有替代链接吗?请帮忙

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

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

最佳答案

最近 Google 关闭了 v1 API。 V2 API 现在需要一个 key ,流式 v2 API 限制为每天 50 个请求。您可以获得此处所述的 key :

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

但是,不保证无限使用。

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

关于android - 谷歌语音 Api v1 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23511015/

相关文章:

Android 内存泄漏 - setBackgroundDrawable - 带有图像

google-api - Google Drive API - 列出多种 MIME 类型

java - 从 GoogleIdToken 获取用户个人资料

php - 尝试验证 Google Play 上的应用内购买时出错

c# - 如何检测谁在说话

speech-recognition - 除了 Google 之外,还有其他语音识别 API 可以返回临时结果吗?

java - WebView 中的 Android 方向更改

java - 如何让 ViewPager 全屏显示?

android - 从字符串创建和显示 SVG

c# - Microsoft Speech - 识别和提取可变长度的数字