mp3's

I got JLayer to try to help me load in my MP3 clips and with much tinkering I was able to get a Player or an AdvancedPlayer created. I held on to my byte[]'s so that I could re-create Player’s when I wanted to play the sounds multiple times, but it was very slow.

I’ve read through threads here talking about creating a Clip with a byte[] to play and that that’s fast, but to do that you need the appropriate AudioFormat to make it work, and I can’t figure out how to get that. I’ve looked through all the classes in JLayer and I can’t find anything to help me.

I tried looking at JOrbis and it was so ugly to read my eyes hurt looking at the code. Same result with their webpage. So no luck there either.

Could anyone give me some advice on how to get some audio playing at practical speeds? Thanks!

Well I’ve found how to get an AudioFormat, but for some reason I can’t get it working still:

InputStream inputstream = url.openStream();
BasicMP3FileReader mp = new BasicMP3FileReader();
audioInputStream = mp.getAudioInputStream(inputstream);

Yields
javax.sound.sampled.UnsupportedAudioFileException: incorrect magic number 1228931072

This will give me a working Player:

InputStream in = audioURL.openStream();
AudioDevice dev = FactoryRegistry.systemRegistry().createAudioDevice();
Player player = new Player(in, dev);

But with that I can never get an AudioFormat out of it. So to sum it up, I think I may have gotten closer but it still doesn’t work.

This is strange, it seems Flash can do short and long sounds beautifully in a web page, but I haven’t heard of anyone managing it in a Java Applet. Surely Java isn’t outdone by Flash is it? Hopefully help for me is on its way!

I don’t know if you want to play your audio in an applet or in a stand alone application … if you want to play your audio in an applet you can use the interface AudioClip from the package java.applet

AudioClip soundToPlay;

soundToPlay = getAudioClip( getCodeBase(), “nameofthefile.wav”);

soundToPlay.play();

Hope it help you.

I have played with AudioClips, but my real desire is to read in mp3’s. If I can get them in as AudioClips I’ll be happy with that. Perhaps the mp3 format isn’t even the way I should go? Is there a better format I should be using that can compress well but is still widely and easily used in Java?

Yes, ogg vorbis. It’s royality free and there is also a nice library. And besides it sounds much better than mp3 especially with low bitrates.

http://www.jcraft.com/jorbis/

edit:

an encoder is here:
http://www.vorbis.com/download.psp

simple to use:
http://www.vorbis.com/download_win.psp (oggdropXPd)

Thanks! I’ll check that out

Yes, Ogg is a good audio format.
It’s been used by a few commercial games and it’s being used by the masters of gaming 3d graphics, ID, for their Doom3 (see here).