对于V1 speechrecognizer API,Alexa语音服务响应返回0字节

时间:2017-01-15 00:59:41

标签: java alexa-voice-service

我正在努力让V1 speechrecognizer API与我的Java客户端一起工作,但我想我错过了一些东西,因为我无法从我的POST请求获得AVS的任何响应。整个代码段如下所示。

希望有人可以指出我的错误。

以下是我的应用的主要功能:

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

1 个答案:

答案 0 :(得分:0)

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