audio – mp3 to wav conversion in java – Stack Overflow

mp3 to wav conversion in java

My code to convert mp3 to wav is:

package audio1;import java.io.File;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;public class NewClass {    public static void main(String [] args){        try{            AudioFileFormat inputFileFormat = AudioSystem.getAudioFileFormat(new File("c:\1.mp3"));            AudioInputStream ais = AudioSystem.getAudioInputStream(new File("c:\1.mp3"));            AudioFormat audioFormat = ais.getFormat();            System.out.println("File Format Type: "+inputFileFormat.getType());            System.out.println("File Format String: "+inputFileFormat.toString());            System.out.println("File lenght: "+inputFileFormat.getByteLength());            System.out.println("Frame length: "+inputFileFormat.getFrameLength());            System.out.println("Channels: "+audioFormat.getChannels());            System.out.println("Encoding: "+audioFormat.getEncoding());            System.out.println("Frame Rate: "+audioFormat.getFrameRate());            System.out.println("Frame Size: "+audioFormat.getFrameSize());            System.out.println("Sample Rate: "+audioFormat.getSampleRate());            System.out.println("Sample size (bits): "+audioFormat.getSampleSizeInBits());            System.out.println("Big endian: "+audioFormat.isBigEndian());            System.out.println("Audio Format String: "+audioFormat.toString());            AudioInputStream encodedASI = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais);            try{                int i = AudioSystem.write(encodedASI, AudioFileFormat.Type.WAVE, new File("c:\converted.wav"));                System.out.println("Bytes Written: "+i);            }catch(Exception e){                e.printStackTrace();            }        }catch(Exception e){            e.printStackTrace();        }    }}

But I am getting following output:

File Format Type: MP3File Format String: MP3 (.mp3) file, byte length: 9631340, data format: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, , frame length: 10030File lenght: 9631340Frame length: 10030Channels: 2Encoding: MPEG1L3Frame Rate: 41.666668Frame Size: -1Sample Rate: 48000.0Sample size (bits): -1Big endian: trueAudio Format String: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, java.lang.ArrayIndexOutOfBoundsException: 1        at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream$DMAISObuffer.append(MpegFormatConversionProvider.java:386)        at javazoom.jl.decoder.Obuffer.appendSamples(Unknown Source)        at javazoom.jl.decoder.SynthesisFilter.compute_pcm_samples(Unknown Source)        at javazoom.jl.decoder.SynthesisFilter.calculate_pcm_samples(Unknown Source)        at javazoom.jl.decoder.LayerIIIDecoder.decode(Unknown Source)        at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(Unknown Source)        at javazoom.jl.decoder.Decoder.decodeFrame(Unknown Source)        at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream.execute(MpegFormatConversionProvider.java:307)        at org.tritonus.share.TCircularBuffer.read(TCircularBuffer.java:138)        at org.tritonus.share.sampled.convert.TAsynchronousFilteredAudioInputStream.read(TAsynchronousFilteredAudioInputStream.java:194)        at javax.sound.sampled.AudioInputStream.read(AudioInputStream.java:292)        at com.sun.media.sound.PCMtoPCMCodec$PCMtoPCMCodecStream.read(PCMtoPCMCodec.java:506)        at com.sun.media.sound.SunFileWriter$NoCloseInputStream.read(SunFileWriter.java:199)        at java.io.SequenceInputStream.read(SequenceInputStream.java:208)        at java.io.SequenceInputStream.read(SequenceInputStream.java:211)        at java.io.InputStream.read(InputStream.java:101)        at com.sun.media.sound.WaveFileWriter.writeWaveFile(WaveFileWriter.java:247)        at com.sun.media.sound.WaveFileWriter.write(WaveFileWriter.java:145)        at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1354)        at audio1.NewClass.main(NewClass.java:33)

Can anyone help me what I am doing wrong?

Here sourceBytes represents MP3 file or WAV file. audioFormat is PCM format in which you want conversion. Also we need to put mp3spi.jar, tritonus_mp3.jar, jl*.jar, tritonus_share.jar from javazoom.com in classpath. Hope this may help to other.

Java 7 version:

public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception {    if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){        throw new IllegalArgumentException("Illegal Argument passed to this method");    }    try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes);         final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {        AudioFormat sourceFormat = sourceAIS.getFormat();        AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false);        try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS);             final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS);             final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {            byte [] buffer = new byte[8192];            while(true){                int readCount = convert2AIS.read(buffer, 0, buffer.length);                if(readCount == -1){                    break;                }                baos.write(buffer, 0, readCount);            }            return baos.toByteArray();        }    }}

Maven:

<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>mp3spi</artifactId>
    <version>1.9.5-1</version>
</dependency>
<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>jlayer</artifactId>
    <version>1.0.1-1</version>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>

此条目发表在Uncategorized分类目录,贴了标签。将固定链接加入收藏夹。