我正在使用Microsoft Azure语音文本发送REST API。根据文档,REST API支持Ogg和Wav格式。但是,当我发送对OGG音频文件的请求时,出现400-错误的请求错误。
我正在使用以下代码来准备请求,而这适用于WAV音频格式:
String url= "https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=simple";
private void connect(String extension) throws IOException {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (extension.equalsIgnoreCase(Constants.WAV))
connection.setRequestProperty("Content-type", "audio/wav; codecs=\"audio/pcm\"; samplerate=16000");
else if (extension.equalsIgnoreCase(Constants.OGG))
connection.setRequestProperty("Content-type", "audio/ogg; codecs=\"audio/opus\"");
connection.setRequestProperty("Accept", "application/json;text/xml");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setRequestProperty("Transfer-Encoding", "chunked");
connection.setRequestProperty("Expect", "100-continue");
connection.setChunkedStreamingMode(0); // 0 == default chunk size
connection.connect();
}
使用此文件上传文件:
private void upload(InputStream inputStream) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
}}
对于WAV和OGG格式,API都可以正常工作。该请求也很好。问题可能出在请求中发送的音频文件。如果要使用OGG音频文件格式,则音频文件必须具有以下属性:OGG(编解码器:Opus,比特率:16位,采样率:16 kHz,Chanel:单声道)
否则,您将收到400错误的请求错误。确保编解码器为Opus,因为大多数OGG文件都具有API不支持的Vorbis编解码器。
您可以使用该网站转换所需格式的音频文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句