Playing mp3

Im trying to play an mp3 but it throws UnsupportedAudioFileException


import javax.sound.sampled.*;
import java.io.*;
public class mp3play{
	public static void main(String args[]) throws IOException{
		File file = new File("clip.mp3");
		AudioFileFormat baseFileFormat = null;
		AudioFormat baseFormat = null;
		try{
			baseFileFormat = AudioSystem.getAudioFileFormat(file);                         //     <----------     here
		}	catch(UnsupportedAudioFileException ex){
			System.out.println("Audio File not Supported");   
		}
		baseFormat = baseFileFormat.getFormat();
		AudioFileFormat.Type type = baseFileFormat.getType();
		float frequency = baseFormat.getSampleRate();
		AudioInputStream in=null;
		try{
			in= AudioSystem.getAudioInputStream(file);
		}	catch(UnsupportedAudioFileException ex){}
		AudioInputStream din = null;
		baseFormat = in.getFormat();
		AudioFormat decodedFormat =
    	new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false);
		din = AudioSystem.getAudioInputStream(decodedFormat, in);
		try{
			rawplay(decodedFormat, din);
		}	catch(LineUnavailableException ex){}
		in.close();
	}	
	private static void rawplay(AudioFormat targetFormat,AudioInputStream din)
   		throws IOException, LineUnavailableException{
 		byte[] data = new byte[4096];
  		SourceDataLine line = getLine(targetFormat);
  		if (line != null){
    		line.start();
    		int nBytesRead = 0, nBytesWritten = 0;
    		while (nBytesRead != -1){
        		nBytesRead = din.read(data, 0, data.length);
        		if (nBytesRead != -1)
            	nBytesWritten = line.write(data, 0, nBytesRead);
    		}
    		line.drain();
    		line.stop();
    		line.close();
    		din.close();
  		}
	}
	private static SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
  		SourceDataLine res = null;
  		DataLine.Info info =
    		new DataLine.Info(SourceDataLine.class, audioFormat);
  		res = (SourceDataLine) AudioSystem.getLine(info);
  		res.open(audioFormat);
  		return res;
	}
}	

Here is the source and the clip: http://www.mediafire.com/?ecvm4ldfmz9

JavaSound doesn’t support mp3.
I suggest to use .ogg instead, using the jorbis library: http://www.jcraft.com/jorbis/index.html

weird cause i got that code from the internet and the file was an mp3.

[quote]I suggest to use .ogg instead, using the jorbis library
[/quote]
Ohh… those JAR-library-things… they never work for me…

JLayer is the lib for MPEG-Layer 3 yet :slight_smile:

jorbis is widely used, I still suggest to give it a try before dismissing it.

I spent like 2 hrs on trying to understand the JOrbisPlayer.java and still dont know how i can implement all that stuff in my game so that it can play .ogg files.
Does anyone have an example of a game that uses it?

A tutorial on playing OGG files in Java: http://www.cokeandcode.com/node/325

If you’re using Java Sound you might be more interested in EasyOgg.

Kev

What MP3 to OGG converter are you guys using?

I’ve tried several of the freewares I’ve found using “free mp3 to ogg” google search, but they all basically suck. One converted my mp3 song into some 2x speed audio file, and after I used another converter then Jogg could not read it (not a valid ogg file).

No proper ones out there?

Audacity does this I believe, though MediaMonkey is also good

What MP3 to OGG converter are you guys using?

None. Converting from one lossy format to another is futile. Well, of course it can be done, but the artifacts introduced by mp3 compression are harder to compress than the original. So, basically you lose some quality for things you don’t actually want.

The proper way to do it is to compress the original (lossless) data again. It’s pretty easy with OggDropXPd for example. The Foobar audio player is also an option - you can convert all kinds of source material to Ogg Vorbis via rmb->convert.

Oh and don’t use the bitrate control stuff. Use the quality setting instead. The -1 setting works for most game related stuff very well. A 35 second mono BGM loop can get as small as 210kb with q=-1 and it still sounds pretty good.

A big advantage of Ogg Vorbis is the subjective good quality at low bitrates. It only sounds a bit damped whereas mp3 starts to create painful “edges” (eg high hats are pretty annoying with mp3 artifacts ).

Ok, ive given up on playing ogg or mp3. I use midi’s now. I have one problem though:
If a midi is playing in an applet in a separate tab and i close that tab, its still playing. ???

You have to stop the playing of the midi-file yourself in the stop()-method of your applet. See http://java.sun.com/docs/books/tutorial/deployment/applet/index.html for details on the applet’s lifecycle.