Tutorial :Is there a way to make JavaSound to use the codecs installed on your SO?



Question:

I am using java Sound with the following code:

public static void main(String[] args) throws Exception  {      JFrame frame = new JFrame();      frame.setSize(200,200);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      frame.setVisible(true);      JFileChooser fc = new JFileChooser();      fc.showOpenDialog(null);      File f = fc.getSelectedFile();      AudioInputStream ais = AudioSystem.getAudioInputStream(f);      AudioFormat format = ais.getFormat();      AudioFormat decodedFormat = new AudioFormat(                  AudioFormat.Encoding.PCM_SIGNED,  // Encoding to use                      format.getSampleRate(),           // sample rate (same as base format)                      16,               // sample size in bits (thx to Javazoom)                      format.getChannels(),             // # of Channels                      format.getChannels()*2,           // Frame Size                      format.getSampleRate(),           // Frame Rate                      false                 // Big Endian              );      SourceDataLine line = AudioSystem.getSourceDataLine(decodedFormat);      AudioInputStream dais = AudioSystem.getAudioInputStream(decodedFormat, ais);      line.open(decodedFormat);      line.start();      byte[] b = new byte[1024];      int i=0;      while(true)      {          i = dais.read(b, 0, b.length);          if(i == -1)              break;          line.write(b, 0, i);      }      line.drain();      line.stop();      line.close();      ais.close();      dais.close();      }  

But to play mp3, tihs requires that I have an SPI on my classpath...its ok, altough I was looking for a way to use the codecs installed in the SO. Is there a way to do that?


Solution:1

I have used this to play MP3s and it was simple.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »