将音频 (WAV) 发送到网络并使用 Java 中的套接字播放
Send Audio (WAV) to web and play using sockets in Java
纠结了好久,上网查了,也没找到解决办法。我几乎是在尝试使用 https://github.com/mrniko/netty-socketio 发送一个 wav。我通过将 WAV 转换为二进制文件(读入后)然后使用 socket
将其推送到前端来做到这一点
问题在于数据已发送,它被转换为 blob,但 blob 无法播放,浏览器设置 Uncaught (in promise) DOMException: Failed to load because no找到支持的来源。 错误。
有什么想法吗?有多个可能的故障点,但我想不通。
Server.JAVA
File file = new File("src/main/resources/test.wav");
AudioInputStream in;
try{
try{
in = AudioSystem.getAudioInputStream(file);
}catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
}catch (UnsupportedAudioFileException e) {
System.out.println("Bad Audio File error");
e.printStackTrace();
return;
}
//CONVERT TO BYTE ARRAY
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
try{
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
}catch (java.io.IOException e) {
System.out.println("Can't read into buffer");
e.printStackTrace();
return;
}
final byte[] audio = buffer.toByteArray();
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
@Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
Client.js
var socket = io.connect('http://localhost:9092');
socket.emit('updateCoordinates');
socket.on('sound', function(file) {
console.log(file)
console.log("recieved");
var arrayBuffer = new Uint8Array(file).buffer;
console.log(arrayBuffer);
var blob = new Blob([arrayBuffer], {type : 'audio/wav'});
console.log(blob);
const audioUrl = URL.createObjectURL(blob);
const audio = new Audio(audioUrl);
audio.play();
});
好的。我想到了。 AudioSystem 剥离了重要元数据的 wav,因此前端无法读取它。服务器工作的更新代码是
Path path = Paths.get("src/main/resources/test.wav");
final byte[] audio;
try{
audio = Files.readAllBytes(path);
}
catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
@Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}
纠结了好久,上网查了,也没找到解决办法。我几乎是在尝试使用 https://github.com/mrniko/netty-socketio 发送一个 wav。我通过将 WAV 转换为二进制文件(读入后)然后使用 socket
将其推送到前端来做到这一点问题在于数据已发送,它被转换为 blob,但 blob 无法播放,浏览器设置 Uncaught (in promise) DOMException: Failed to load because no找到支持的来源。 错误。
有什么想法吗?有多个可能的故障点,但我想不通。
Server.JAVA
File file = new File("src/main/resources/test.wav");
AudioInputStream in;
try{
try{
in = AudioSystem.getAudioInputStream(file);
}catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
}catch (UnsupportedAudioFileException e) {
System.out.println("Bad Audio File error");
e.printStackTrace();
return;
}
//CONVERT TO BYTE ARRAY
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
try{
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
}catch (java.io.IOException e) {
System.out.println("Can't read into buffer");
e.printStackTrace();
return;
}
final byte[] audio = buffer.toByteArray();
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
@Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
Client.js
var socket = io.connect('http://localhost:9092');
socket.emit('updateCoordinates');
socket.on('sound', function(file) {
console.log(file)
console.log("recieved");
var arrayBuffer = new Uint8Array(file).buffer;
console.log(arrayBuffer);
var blob = new Blob([arrayBuffer], {type : 'audio/wav'});
console.log(blob);
const audioUrl = URL.createObjectURL(blob);
const audio = new Audio(audioUrl);
audio.play();
});
好的。我想到了。 AudioSystem 剥离了重要元数据的 wav,因此前端无法读取它。服务器工作的更新代码是
Path path = Paths.get("src/main/resources/test.wav");
final byte[] audio;
try{
audio = Files.readAllBytes(path);
}
catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
@Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}