注:PCM编码格式的文件(音频流)称为裸流,即纯粹的音频数据,将这些数据直接丢进混响器即可直接播放。
import java.io.FileInputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
/**
* 播放pcm音频
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
if(args.length==0) {
args = new String[1];
args[0] = "F:\\music/a.pcm";//文件路径
}
try {
FileInputStream fis = new FileInputStream(args[0]);
AudioFormat.Encoding encoding = new AudioFormat.Encoding("PCM_SIGNED");
AudioFormat format = new AudioFormat(encoding,8000, 16, 1, 2, 8000 ,false);//编码格式,采样率,每个样本的位数,声道,帧长(字节),帧数,是否按big-endian字节顺序存储
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
byte[] b = new byte[256];
try {
while(fis.read(b)>0) {
auline.write(b, 0, b.length);
System.out.println(1);
}
auline.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}