【发布时间】:2013-02-18 21:02:36
【问题描述】:
我从服务器收到了一个字节数组,我知道它可以完美地连接和发送。这是我尝试从字节数组播放声音的时候。
这是我必须播放的声音。
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);
}
}
获取音频格式:
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);
}
如何从byte[] 播放音乐?
【问题讨论】:
-
你的意思是
Byte[]还是byte[]? -
它们是相对相同的......但我的意思是字节[]。
-
不,它们不一样。
Byte[]是java.lang.Byte类型的引用数组,byte[]是原始 8 位有符号整数值数组。 -
如需尽快获得更好的帮助,请发帖SSCCE。
-
会不会像缺少
speaker.start()电话一样简单?
标签: java client-server javasound audio