根据官方提供的 WebIATWS
工具扩展修改,接入了讯飞的语音听写(STT)服务
讯飞认证配置
public class XFAuthorityConfig {
public static final String hostUrl = "https://iat-api.xfyun.cn/v2/iat";
public static final String apiKey = "xxxx";
public static final String apiSecret = "xxx";
public static final String appid = "5ede17d7";
}
封装监听器
public class WrapListener extends WebSocketListener {
private static final Logger LOGGER = LoggerFactory.getLogger(WrapListener.class);
// 下面三个参数是我根据需要新增的
// file 是要听写的音频文件,而language 支持 zh-CN,en-US]
// result 是返回结果
private InputStream file;
private String language;
private String result;
private String appId = XFConfig.appid;
public static final int StatusFirstFrame = 0;
public static final int StatusContinueFrame = 1;
public static final int StatusLastFrame = 2;
public static final Gson json = new Gson();
Decoder decoder = new Decoder();
// 开始时间
private static Date dateBegin = new Date();
// 结束时间
private static Date dateEnd = new Date();
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
public InputStream getFile() {
return file;
}
public void setFile(InputStream file) {
this.file = file;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
int frameSize = 1280;
int intervel = 40;
int status = 0;
try {
byte[] buffer = new byte[frameSize];
// 发送音频
end:
while (true) {
int len = file.read(buffer);
if (len == -1) {
status = StatusLastFrame;
}
switch (status) {
case StatusFirstFrame:
JsonObject frame = new JsonObject();
JsonObject business = new JsonObject()
JsonObject common = new JsonObject();
JsonObject data = new JsonObject();
// 填充common
common.addProperty("app_id", appId);
//填充business
business.addProperty("language", language);
business.addProperty("domain", "iat");
business.addProperty("accent", "mandarin");//中文方言请在控制台添加试用,添加后即展示相应参数值
business.addProperty("dwa", "wpgs");
data.addProperty("status", StatusFirstFrame);
data.addProperty("format", "audio/L16;rate=8000");
data.addProperty("encoding", "raw");
data.addProperty("audio", Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
//填充frame
frame.add("common", common);
frame.add("business", business);
frame.add