从byte []数组播放声音 [英] Playing sound from a byte[] array

查看:176
本文介绍了从byte []数组播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器接收到一个字节数组,我知道连接和发送完美。

I've recieved a byte array from the server and I know that connects and sends perfectly. It's when I try and play the sound from the byte array.

这是我要播放的声音。

SourceDataLine speaker = null;
try {
    DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, getAudioFormat(samplerate));
    speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
} catch (LineUnavailableException e) {
    e.printStackTrace();
}
int nBytesRead = 0;
    while (nBytesRead != -1) {
    if (nBytesRead >= 0) {
         speaker.write(bytes, 0, nBytesRead);
    }
}

getAudioFormat:

getAudioFormat:

private AudioFormat getAudioFormat(float sample) {
    int sampleSizeBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    return new AudioFormat(sample, sampleSizeBits, channels, signed, bigEndian);
}

如何从

推荐答案

我没有看到你从你的声音字节数组读取的时间循环。他们的方式你设置,应该可能有这样的东西:

I don't see where you are reading from your sound byte array in your while loop. They way you are set up, there should probably be something along these lines:

while (nBytesRead = soundDataArray.read(bytes) != 1)

...假设你设置了读方法,字节从读取命令接收数据。然后write()方法将重复填充'bytes'来发送。

...assuming you have the read method set up so that the buffer called 'bytes' receives the data from the read command. Then the write() method will have 'bytes' repeatedly populated to send.

当然,'bytes'只是一个在while循环中使用的缓冲区,字节数组与源声音。

Of course, 'bytes' is just a buffer only used in the while loop, NOT the byte array with the source sound.

有时,read方法有两个输入,如: .read(bufferArray,bytesToRead);
,其中在ak或几个k的范围内的值是公共的。 (bufferArray.length == bytesToRead)

Sometimes the read method has two inputs, as in: .read(bufferArray, bytesToRead); where values in the range of a k or several k are common. (bufferArray.length == bytesToRead)

这篇关于从byte []数组播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆