Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸
前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i
音频解析方法:
1 public static int toInt(byte[] b) { 2 return ((b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0] << 0)); 3 } 4 5 public static short toShort(byte[] b) { 6 return (short)((b[1] << 8) + (b[0] << 0)); 7 } 8 9 10 public static byte[] read(RandomAccessFile rdf, int pos, int length) throws IOException { 11 rdf.seek(pos); 12 byte result[] = new byte[length]; 13 for (int i = 0; i < length; i++) { 14 result[i] = rdf.readByte(); 15 } 16 return result; 17 }
音频解析方法调用:
1 public static void main(String[] args) throws IOException { 2 File f = new File("E:/zmj-3011-32779/audio.wav"); 3 RandomAccessFile rdf = null; 4 rdf = new RandomAccessFile(f,"r"); 5 6 System.out.println("声音尺寸: " + toInt(read(rdf, 4, 4))); // 声音尺寸 7 8 System.out.println("音频格式: " + toShort(read(rdf, 20, 2))); // 音频格式 1 = PCM 9 10 System.out.println("声道数: " + toShort(read(rdf, 22, 2))); // 1 单声道 2 双声道 11 12 System.out.println("采样率: " + toInt(read(rdf, 24, 4))); // 采样率、音频采样级别 8000 = 8KHz 13 14 System.out.println("波形的数据量: " + toInt(read(rdf, 28, 4))); // 每秒波形的数据量 15 16 System.out.println("采样帧: " + toShort(read(rdf, 32, 2))); // 采样帧的大小 17 18 System.out.println("采样位数: " + toShort(read(rdf, 34, 2))); // 采样位数 19 20 rdf.close(); 21 22 23 }
控制台打印结果:
参考文章:https://www.iteye.com/blog/mzhj-1068237
总语
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!