gpt4 book ai didi

java - Alexa 语音服务响应为 V1 语音识别器 API 返回 0 字节

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:42 26 4
gpt4 key购买 nike

我正在尝试让 V1 语音识别器 API 与我的 java 客户端一起使用,但我认为我遗漏了一些东西,因为我无法从 AVS 获取对我的 POST 请求的任何响应。完整的代码片段如下所示。

希望有人指出我的错误。

这是我的应用程序的主要功能:

public class App 
{
private static String requestURL = "https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize";

public static void main( String[] args )
{
try {

MultipartUtility mpu = new MultipartUtility(requestURL, "UTF-8", AvsRequest.getToken(), AvsRequest.getBoundary());
mpu.addRequestStart();
mpu.addData("tts_hello_how_are_you_doing_today.wav");
mpu.addRequestEnd();
mpu.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
}

这是请求正文的实用方法:

public class AvsRequest {
private static final String BOUNDARY = "BOUNDARY1234";
private static final String BOUNDARY_DASHES = "--";
private static final String NEWLINE = "\r\n";
private static final String METADATA_CONTENT_DISPOSITION = "Content-Disposition: form-data; name=\"metadata\"";
private static final String METADATA_CONTENT_TYPE="Content-Type: application/json; charset=UTF-8";
private static final String AUDIO_CONTENT_TYPE="Content-Type: audio/L16; rate=16000; channels=1";
private static final String AUDIO_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"audio\"";

private static final String METADATA="{\"messageHeader\": {\"deviceContext\":[{\"name\":\"playbackState\", \"namespace\":\"AudioPlayer\", \"payload\":{\"streamId\":\"\", \"offsetInMilliseconds\":\"0\", \"playerActivity\":\"IDLE\"}}]},\"messageBody\": {\"profile\": \"alexa-close-talk\",\"locale\": \"en-us\",\"format\": \"audio/L16; rate=16000; channels=1\"}}";

private static final String TOKEN = "Atza|I....";

public static String getRequestStart(){

StringBuilder str = new StringBuilder();
str.append(BOUNDARY_DASHES);
str.append(BOUNDARY);
str.append(NEWLINE);
str.append(METADATA_CONTENT_DISPOSITION);
str.append(NEWLINE);
str.append(METADATA_CONTENT_TYPE);
str.append(NEWLINE);
str.append(NEWLINE);
str.append(METADATA);
str.append(NEWLINE);
str.append(NEWLINE);
str.append(BOUNDARY_DASHES);
str.append(BOUNDARY);
str.append(NEWLINE);
str.append(AUDIO_CONTENT_DISPOSITION);
str.append(NEWLINE);
str.append(AUDIO_CONTENT_TYPE);
str.append(NEWLINE);

return str.toString();
}

public static String getRequestEnd(){

StringBuilder str = new StringBuilder();
str.append(NEWLINE);
str.append(NEWLINE);
str.append(BOUNDARY_DASHES);
str.append(BOUNDARY);
str.append(BOUNDARY_DASHES);
str.append(NEWLINE);

return str.toString();
}

public static String getBoundary() {
return BOUNDARY;
}

public static String getToken() {
return TOKEN;
}
}

这是 Multipart 请求类:

public class MultipartUtility {

private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
private String oauth2Token;
private URL url;


public MultipartUtility(String requestURL, String charset, String token, String boundary) throws IOException {


this.charset = charset;
this.oauth2Token = token;
this.url = new URL(requestURL);

httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestProperty("Authorization", "Bearer " +token);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),false);

}

public void addRequestStart(){
writer.append(AvsRequest.getRequestStart());
writer.append("Content-Transfer-Encoding: binary").append("\r\n");
System.out.println("POST REQUEST START: \n" + AvsRequest.getRequestStart());

}

public void addData(String fileName) throws IOException{
FileInputStream inputStream = new FileInputStream(fileName);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
writer.write(buffer.toString());
}
writer.flush();
inputStream.close();
}



public void addRequestEnd(){

writer.append(AvsRequest.getRequestEnd());
System.out.println("POST REQUEST END: " +AvsRequest.getRequestEnd());
writer.flush();
writer.close();
}

public void finish1() throws IOException {
InputStream response = null;
try {
writer.close();
response = httpConn.getInputStream();
System.out.println("Response Size:\n"+ response.available());
OutputStream fos = new FileOutputStream(new File("response.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
String line = null;
while ((line = reader.readLine()) != null) {
fos.write(line.getBytes());
fos.flush();
}
reader.close();
fos.close();
httpConn.disconnect();

} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

在发送不适合 Alexa 语音服务的音频格式时,我得到了类似的结果。在测试发送到 https 的音频文件时,我使用 Audacity 以单声道和 16000 hz 格式化并测试音频,然后返回有效的响应。以立体声发送音频似乎返回零字节结果。

关于java - Alexa 语音服务响应为 V1 语音识别器 API 返回 0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41656725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com