java解析音频文件/音乐播放器

说明:

主要是用了Java sound(刚开始我也不知道,百度什么的,查查的就明白了大笑,或者直接参考jdk的API文档),我没有打印所有的信息,想要什么参考官方API文档自己加,在此附上官方的demo(可直接下载):点击打开链接,此程序可解析WAV和MP3格式的音频文件,如果你只需要解析MP3文件的头部信息没必要用这个(网上一大堆)。此程序也可以自己搞好页面做播放器使用,不比比了,直接上代码。


代码:

public class ReadSoundMP3{
    public static void main(String[] args) throws Exception{
        //假设音量条100,音量默认为80
        double value = 80 / 100.0;
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("E:\\地尽头.mp3"));
        AudioFormat format = stream.getFormat();
        System.out.println("具有此格式的声音每秒播放或录制的样本数:"+ format.getSampleRate());
        System.out.println("每个具有此格式的声音帧包含的字节数:"+ format.getFrameSize());
        if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,format.getSampleRate(), 16, format.getChannels(),format.getChannels() * 2, format.getSampleRate(), false);
            stream = AudioSystem.getAudioInputStream(format, stream);
        }
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
        SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceDataLine.open(stream.getFormat(), sourceDataLine.getBufferSize());
        sourceDataLine.start();
        List<Control> cons = new ArrayList<>();
        Control [] ss = sourceDataLine.getControls();
        Control t = null;
        for (int i=0;i<ss.length;i++){
            t = ss[i];
            cons.add(t);
            System.out.println("控件类型:"+t.getType());
        }
        FloatControl adin = (FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
        System.out.println("当前值:"+ adin.getValue());
        System.out.println("分辨率或粒度:"+ adin.getPrecision());
        System.out.println("中点值的标签:"+ adin.getMidLabel());
        System.out.println("最大值得标签:"+ adin.getMaxLabel());
        System.out.println("最大值:"+ adin.getMaximum());
        System.out.println("最小值得标签:"+ adin.getMinLabel());
        System.out.println("最小值:"+ adin.getMinimum());
        System.out.println("单位:"+ adin.getUnits());
        System.out.println("音量等级:"+ sourceDataLine.getLevel());
        float dB = (float) (Math.log(value)/Math.log(10.0)*20.0);
        System.out.println("计算后:"+ dB);
        //设置音量大小
        adin.setValue(dB);

        int numRead = 0;
        byte[] buf = new byte[sourceDataLine.getBufferSize()];
        System.out.println("字节流:"+ buf.toString());
        System.out.println("字节流长度:"+ buf.length);
        while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
            int offset = 0;
            while (offset < numRead) {
                offset += sourceDataLine.write(buf, offset, numRead - offset);
            }
            System.out.println("音频数据中的当前位置(样本帧/微秒):"+sourceDataLine.getFramePosition() + " " + sourceDataLine.getMicrosecondPosition());
        }
        sourceDataLine.drain();
        sourceDataLine.stop();
        sourceDataLine.close();
        stream.close();
    }
}


maven中jar包引入:

<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>mp3spi</artifactId>
	<version>1.9.5-1</version>
</dependency>
<dependency>
	<groupId>javazoom</groupId>
	<artifactId>jlayer</artifactId>
	<version>1.0.1</version>
</dependency>
<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>tritonus-share</artifactId>
	<version>0.3.7-2</version>
</dependency>

控制台打印:




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值