Azure STT 给出错误:HttpResponseProxy{HTTP/1.1 400 错误请求 [内容类型:text/plain; charset=utf-8,日期:2024 年 2 月 2 日星期五 14:33:46 GMT,服务器:Kestrel,传输编码:分块,严格传输安全:max-age=31536000; includeSubDomains] ResponseEntityProxy{[内容类型:文本/纯文本;字符集=utf-8,分块:true]}}。 HttpClient httpclient = HttpClients.createDefault(); URIBuilder 构建器 = new URIBuilder(env.getProperty("voice.text.api")); BASE64DecodedMultipartFile 音频 = 新 BASE64DecodedMultipartFile(Base64.getDecoder().decode(audioReq));
builder.setParameter("language", sourceLang);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader(CONTENT_TYPE, AUDIO_WAV_TYPE);
request.setHeader("Ocp-Apim-Subscription-Key", env.getProperty("Voice.SubKey1"));
//request.setHeader("Accept","application/json");
request.setEntity(new FileEntity(convert(audio), ContentType.APPLICATION_OCTET_STREAM));
HttpResponse response = null;
try {
response = httpclient.execute(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
代理 400 错误是由于配置不正确或发送到 Azure 语音转文本服务的数据无效而发生的。
要修复该错误,请确保使用 Azure 语音转文本服务的正确端点 URI,以及有效的语音密钥以及 .wav 文件中格式正确的音频数据。
以下是正确的端点 URI:
https://<speech_region>.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US
以下代码已使用正确的端点 URI 和语音密钥进行更新,使其能够将语音转换为文本而不会出现任何错误。
代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
import java.util.Scanner;
public class SpeechToTextExample {
public static void main(String[] args) {
HttpClient httpclient = HttpClients.createDefault();
URIBuilder builder = null;
try {
builder = new URIBuilder("https://<speech_region>.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
String filePath = "path/to/.wav file";
String sourceLang = "en-US";
builder.setParameter("language", sourceLang);
URI uri = null;
try {
uri = builder.build();
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "audio/wav");
request.setHeader("Ocp-Apim-Subscription-Key", "<speech_key>");
try {
File audioFile = new File(filePath);
request.setEntity(new FileEntity(audioFile, ContentType.create("audio/wav")));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
Scanner scanner = new Scanner(entity.getContent());
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
输出:
运行成功,语音转换为文本如下图。
{"RecognitionStatus":"Success","Offset":1100000,"Duration":72600000,"DisplayText":"Hello, this is a test of the speech synthesis service."}