Java MP3转PCM

Java MP3转PCM

  • [1 添加依赖](#1 添加依赖)
  • [2 Java 代码](#2 Java 代码)

1 添加依赖

java 复制代码
<dependency>
	<groupId>com.googlecode.soundlibs</groupId>
	<artifactId>mp3spi</artifactId>
	<version>1.9.5.4</version>
</dependency>

2 Java 代码

java 复制代码
package com.xu.music.player.player;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

import java.io.File;

/**
 * Java 音频播放
 *
 * @author hyacinth
 * @date 2019年10月31日19:06:39
 */
public class NewTest {

    public static void main(String[] args) throws Exception {
        toPcm("D:\\Kugou\\梦涵 - 加减乘除.mp3", "C:\\Users\\xuyq\\Desktop\\梦涵 - 加减乘除.pcm");
    }

    /**
     * Java Music mp3 转PCM
     *
     * @param iPath mp3文件路径
     * @param oPath 保存PCM文件路径
     * @date 2019年10月25日 下午12:28:41
     */
    public static void toPcm(String iPath, String oPath) throws Exception {
        AudioInputStream stream = new MpegAudioFileReader().getAudioInputStream(new File(iPath));

        AudioFormat format = stream.getFormat();
        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(),
                format.getChannels() * 2, format.getSampleRate(), false);

        stream = AudioSystem.getAudioInputStream(format, stream);

        AudioSystem.write(stream, AudioFileFormat.Type.WAVE, new File(oPath));
    }

}