Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸

请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i
音频解析方法:

public static int toInt(byte[] b) {
        return ((b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0] << 0));
    }

    public static short toShort(byte[] b) {
        return (short)((b[1] << 8) + (b[0] << 0));
    }


    public static byte[] read(RandomAccessFile rdf, int pos, int length) throws IOException {
        rdf.seek(pos);
        byte result[] = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = rdf.readByte();
        }
        return result;
    }

音频解析方法调用:

public static void main(String[] args) throws IOException {
     File f = new File("E:/zmj-3011-32779/audio.wav");
        RandomAccessFile rdf = null;
        rdf = new RandomAccessFile(f,"r");

        System.out.println("声音尺寸: " + toInt(read(rdf, 4, 4))); // 声音尺寸

        System.out.println("音频格式: " + toShort(read(rdf, 20, 2))); // 音频格式 1 = PCM

        System.out.println("声道数: " + toShort(read(rdf, 22, 2))); // 1 单声道 2 双声道

        System.out.println("采样率: " + toInt(read(rdf, 24, 4)));  // 采样率、音频采样级别 8000 = 8KHz

        System.out.println("波形的数据量: " + toInt(read(rdf, 28, 4)));  // 每秒波形的数据量

        System.out.println("采样帧: " + toShort(read(rdf, 32, 2)));  // 采样帧的大小

        System.out.println("采样位数: " + toShort(read(rdf, 34, 2)));  // 采样位数

        rdf.close();


    }

控制台打印结果:
效果
参考文章:https://www.iteye.com/blog/mzhj-1068237

个人总结:
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值