在Spring中将原始音频字节发送到Web套接字处理程序的最佳方法是什么?我有以下Web套接字处理程序,可以从客户端正确接收文本消息。
public class TranscribeHandler implements WebSocketHandler {
WebSocketSession session;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
this.session = session;
System.out.println("Connection established Transcribe");
}
@Override
public void handleMessage(WebSocketSession var1, WebSocketMessage<?> var2) throws Exception {
System.out.println("Got message");
}
@Override
public void afterConnectionClosed(WebSocketSession var1, CloseStatus var2) throws Exception {
System.out.println("Connection closed");
}
@Override
public boolean supportsPartialMessages() {
return false;
}}
这是处理麦克风音频的客户端代码
this.ws = new $WebSocket("ws://localhost:8082/transcribe");
//Creates a websocket session with server successfully
var scriptNode = context.createScriptProcessor(256, 1, 1);
var scriptNode.addEventListener('audioprocess', function(e) {
var audioBytes = e.inputBuffer.getChannelData(0);
this.ws.send(audioBytes).publish().connect();
}
我得到的audioBytes是'Float32Array',我将其直接发送到服务器套接字会话。服务器处理程序以字符串格式接收此消息,如下所示:
{ “0”: - 0.0005212430842220783, “1”: - 0.0008100871928036213, “2”: - 0.0008365644607692957, “3”: - 0.0020894776098430157, “4”: - 0.0006563755450770259, ... ... 254 “: - 0.0005547034088522196,” 255" :0.0010016305604949594}
我想将此字符串消息转换为原始字节[]。我怎么做?我想应该有一种简单的方法直接通过套接字发送音频字节而不是我处理字符串格式。如果有人试图这样做,请帮助。感谢。