我使用Google语音Api v1开发了一款应用
https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=&#34 + 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;
}
其他人是否有同样的问题?。
答案 0 :(得分:14)
最近谷歌关闭了v1 API。 V2 API现在需要密钥,流式传输v2 API每天限制为50个请求。您可以按照此处的说明获取密钥:
http://www.chromium.org/developers/how-tos/api-keys
但无法保证无限制使用。
答案 1 :(得分:2)
如果您正在寻找一个Java API检查this。它支持新的Duplex API和V2 API。附带一个方便的教程。如果他们还没有关闭V1,那么在Google关闭之前移植可能是一个好主意。
答案 2 :(得分:0)
Chrome的Speech API无法获得额外的配额。 请改为Cloud Speech API。
请勿发布任何Chromium群组/邮件列表,以了解有关Speech API的问题。