java - 将音频(WAV)发送到Web并使用Java中的套接字播放

标签 java audio blob wav netty-socketio

我已经坚持了一段时间,并且搜寻了互联网,找不到任何解决方案。我几乎尝试使用https://github.com/mrniko/netty-socketio发送wav。为此,我将WAV转换为二进制文件(在读入后),然后使用套接字将其推到前端

问题在于,已发送数据,将其转换为Blob,但该Blob无法播放,浏览器将其定位为Uncaught( promise )DOMException:无法加载,因为未找到受支持的源。错误。

有任何想法吗?可能存在多个故障点,但我无法弄清楚。

服务器.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剥离了重要的元数据,因此前端无法读取它。服务器工作的更新代码为

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();
}

关于java - 将音频(WAV)发送到Web并使用Java中的套接字播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49453831/

相关文章:

java - Android 获取音频文件的时长

java - ORA-00932 : inconsistent datatypes: expected - got clob hibernate/springboot

java - 如何将hibernate映射到JSP View ?

java - 如何存储和修改账户余额:

java - 霍夫曼代码将位写入文件以进行压缩

audio - ffmpeg lavfi 音频失真

java - 如何遍历 2 个列表并检查一个元素的内容是否与不同列表中的另一个元素相同?

android - 从 AudioRecord 计算耗时

c# - Linq查询blob列表,内存占用

mysql - Matlab 损坏 MySQL 中的 blob?