在程序中实现JAVE

时间:2016-02-03 15:36:08

标签: java

我正在尝试使用JAVE http://www.sauronsoftware.it/projects/jave/在Windows上的java程序中将flac转换为mp3。我已经下载了源代码等,我想知道如何将它添加到我的程序中?

1 个答案:

答案 0 :(得分:0)

@Faendol:首先你需要导入最新的JAVE jar,即jave-1.0.2.jar你可以google关于导入jar文件。

现在我已经创建了将wav文件转换为mp3的独立程序。(只是为了简单介绍一下)。测试示例。 同样,您可以为FLAC文件实现它。但是我怀疑你可以用JAVE做到这一点虽然它的网站说是!

    import it.sauronsoftware.jave.AudioAttributes;
    import it.sauronsoftware.jave.Encoder;
    import it.sauronsoftware.jave.EncoderException;
    import it.sauronsoftware.jave.EncodingAttributes;
    import it.sauronsoftware.jave.InputFormatException;

    import java.io.File;
    import java.io.IOException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;

    import javax.crypto.BadPaddingException;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.sound.sampled.UnsupportedAudioFileException;


 public class AudioConverter {

   public static void main(String[] args) throws IllegalArgumentException, InputFormatException, EncoderException, IOException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedAudioFileException{

    String sourceFile = "D:\\source.wav";
    String targetFile = "D:\\target.mp3";


    int samplingRate = 16000;// this could be 8000, 16000 mono or 16000 stereo 
    int channels = 2;// this could be 1 for mono and 2 for stereo
    int bitRate = 190000;// this could be 128, 160, 190 kbps, etc..

    AudioAttributes audio   = new AudioAttributes();
    audio.setCodec("libmp3lame");
    audio.setBitRate(bitRate);
    audio.setChannels(channels);
    audio.setSamplingRate(samplingRate);
    EncodingAttributes ea = new EncodingAttributes();
    ea.setAudioAttributes(audio);
    ea.setFormat("mp3");
    File f = new File(sourceFile);
    Encoder e = new Encoder();

    System.out.println(e.getInfo(f));

    e.encode(f, new File(targetFile), ea);
 }}