vinox
vinox

Reputation: 401

converting audio file in java

I have used below code for converting audio file to wav format with 8000 Hz, 16 bit and mono channel using JAVE

File source = new File("file.mp3");
     File target = new File("soundfile\\file2.wav");
     AudioAttributes audio = new AudioAttributes();
     audio.setCodec("pcm_s16le");
     audio.setBitRate(new Integer(16));
     audio.setChannels(new Integer(1));
     audio.setSamplingRate(new Integer(8000));
     EncodingAttributes attrs = new EncodingAttributes();
     attrs.setFormat("wav");
     attrs.setAudioAttributes(audio);
     Encoder encoder = new Encoder();
     try {
        encoder.encode(source, target, attrs);
        System.out.println("Successfully created"); 
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InputFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (EncoderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It converting but that file is not play in media player. can any one help me to detect the problem in my code.

Upvotes: 1

Views: 4010

Answers (1)

Davetron
Davetron

Reputation: 96

I just tried your code with a random mp3 and the resulting wav played fine.

This is a shot in the dark but I've come across a scenario where the two channels of the source mp3 are inverse of each other. When this gets converted to a single channel, they cancel each other out resulting in a silent wav.

You can check this quickly by setting the conversion to keep 2 channels

audio.setChannels(new Integer(2));

Upvotes: 1

Related Questions