This discussion is archived
1 Reply Latest reply: Mar 4, 2013 8:53 AM by aksarben RSS

Reading Mp3 File

994393 Newbie
Currently Being Moderated
I am reading Mp3 file and want to convert in desired audio format.I am trying Following code.when i read file using "in" while loop run 469 times.while when i run using din i am getting 4601.So why is it so.
i m totally confused here.plz help!!!!!

My code is

import javax.sound.sampled.*;

public class Demo
public static void main(String args[])
try {
File file = new File("D:\\aa.mp3");
AudioInputStream in= AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getChannels() * 2,
byte[] data = new byte[4096];
din = AudioSystem.getAudioInputStream(decodedFormat, in);

Demo n =new Demo();
int nBytesRead = 0, nBytesWritten = 0;
int x=0;
while (nBytesRead != -1)


nBytesRead =, 0, data.length);


catch (Exception e)
//Handle exception.

public SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);;
return res;
  • 1. Re: Reading Mp3 File
    aksarben Journeyer
    Currently Being Moderated
    One thing that jumps out immediately is that you construct din and in differently. So it's no great surprise they behave differently. What do the Javadocs say about these two ways to call AudioSystem.getAudioInputStream()?


  • Correct Answers - 10 points
  • Helpful Answers - 5 points