How can I convert a wav file in java

江枫思渺然 提交于 2019-12-10 18:36:40

问题


How can I convert a wav file in java

AudioFormat targetFormat = new AudioFormat(
                                    sourceFormat.getEncoding(),
                                    fTargetFrameRate,
                                    16,
                                    sourceFormat.getChannels(),
                                    sourceFormat.getFrameSize(),
                                    fTargetFrameRate,
                                    false);

in result Exception :

java.lang.IllegalArgumentException: Unsupported conversion: 
ULAW 8000.0 Hz, **16 bit**, mono, 1 bytes/frame, **from** ULAW 8000.0 Hz, **8 bit**, mono, 1 bytes/frame

it is possible in java?

I need get wav file 16 bit, from 8


回答1:


Here is a method that will convert an 8-bit uLaw encoded binary file into a 16-bit WAV file using built-in Java methods.

public static void convertULawFileToWav(String filename) {
    File file = new File(filename);
    if (!file.exists())
        return;
    try {
        long fileSize = file.length();
        int frameSize = 160;
        long numFrames = fileSize / frameSize;
        AudioFormat audioFormat = new AudioFormat(Encoding.ULAW, 8000, 8, 1, frameSize, 50, true);
        AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file), audioFormat, numFrames);
        AudioSystem.write(audioInputStream, Type.WAVE, new File("C:\\file.wav"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}



回答2:


Look at this one: Conversion of Audio Format it is similar to your issue suggesting looking at http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioSystem.html




回答3:


You can always use FFMPEG, http://ffmpeg.org/, to do the conversion. Your Java program can call FFMPEG to do the conversion.
FFMPEG works on all OS.



来源:https://stackoverflow.com/questions/12586949/how-can-i-convert-a-wav-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!