1.一开始发现8bit的pcm能正常转换,但换成16bit转换出来全是噪音,网上资料也不全,思考了很久,突然想起大小端的问题,进行大小端处理后再进行转换,完美播放!
下面贴出大小端转换方法:
public class BigorLittle {
public static String bigtolittle( String fileName) throws IOException {
File file = new File(fileName); //filename为pcm文件,请自行设置
InputStream in = null;
byte[] bytes = null;
in = new FileInputStream(file);
bytes = new byte[in.available()];//in.available()是得到文件的字节数
int length=bytes.length;
while (length!=1){
long i= in.read(bytes,0,bytes.length);
if(i==-1){
break;
}
length-=i;
}
int dataLength = bytes.length;
int shortlength = dataLength / 2;
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 0, dataLength);
ShortBuffer shortBuffer = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();//此处设置大小端
short[] shorts = new short[shortlength];
shortBuffer.get(shorts, 0, shortlength);
File file1 = File.createTempFile("pcm",null);//输出为临时文件
String pcmtem = file1.getPath();
FileOutputStream fos1 = new FileOutputStream(file1);
BufferedOutputStream bos1 = new BufferedOutputStream(fos1);
DataOutputStream dos1 = new DataOutputStream(bos1);
for (int i = 0; i < shorts.length; i++) {
dos1.writeShort(shorts[i]);
}
dos1.close();
Log.d("gg", "bigtolittle: "+"="+shorts.length);
return pcmtem;
}
2.pcm转wav参考文章:
https://blog.csdn.net/mcgrady_tracy/article/details/52502263
https://blog.csdn.net/JenseaChen/article/details/46883319