我从服务器收到了一个字节数组,我知道它可以完美地连接和发送。这是我尝试从字节数组播放声音的时候。
这是我必须播放的声音。
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[]
?