任何人都可以检查我的代码上是否有错误:这是为了传递Microsoft Speech API的字节,但是我收到了400错误的请求错误。 WAV文件格式是正确的,因为我使用了Microsoft提供的示例。任何帮助将不胜感激!
public void speechToText(){
String Key = "mysubscriptionkey";
try {
File file = new File("filepath", "filename.wav");
URL url = new URL("https://westus.stt.speech.microsoft.com/speech/recognition/interactive/cognitiveservices/v1?langauge=en-US&format=simple");
HttpURLConnection con =(HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.addRequestProperty("Content-type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
con.addRequestProperty("Ocp-Apim-Subscription-Key", Key);
byte[] bytes = ReadFully(file);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(bytes);
Scanner scanner = new Scanner(con.getInputStream());
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
catch (Exception e)
{
e.printStackTrace();
}
-用来读取音频的那个
public byte[] ReadFully(File file){
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int bytesRead;
OutputStream outputstream ;
while ((bytesRead = in.read(buffer)) != -1){
out.write(buffer, 0, bytesRead);
}
}
catch (Exception e){
e.printStackTrace();
}
return out.toByteArray();
}