At work, I'm trying out BeatRoot, an application written in Java, for its BPM estimation capabilities. The data set I'm working with is encoded in MP3 format and getting this Java tool to work with the MP3 data was not an easy ride.

Java does not support MP3 en/decoding by default, which is fine for me, I wouldn't have expected that (MP3 technology is encumbered with patents after all). However, recent versions of Java come with a Java Sound API for reading and writing audio files and it supports basic audio formats like WAV, AU and AIFF. Moreover, Java Sound also provides a plug-in infrastructure for extending support to other audio formats, like MP3 and Ogg Vorbis.

Luckily, BeatRoot uses the Java Sound API, so no panic on that front. The hard part was however getting a Java Sound plug-in for MP3 support and making it work. Googling for a solution was not as satisfying as I expected/hoped ("java" and "mp3" are not very obscure keywords I would think) and the documentation of the stuff I did found was - erm - not always very enlightening.

In the end I found three ways/libraries/plug-ins to get MP3 decoding working and by listing them here, I hope I'll save someone else's time. The following works for me now, July 2009. If you read this from the future, some things could be different (like URLs and versions), but as you are from the future, you should be smart enough to handle this.

  • The MP3 plugin from Sun's Java Media Framework. The zip file I downloaded contained, apart from some readme and license stuff, a jar mp3plugin.jar. The documentation states that this jar should be put in a folder lib/ext under the home directory of your JREs, but I found out that putting it in my CLASSPATH variable was enough to get it working. I guess putting it in the lib/ext folder is required to make it work for the Java Media Framework, but not required to make it work with the Java Sound API.
  • Tritonus is an open source implementation of the Java Sound API with plug-ins for MP3 and Ogg Vorbis among others. To get MP3 support for Java Sound: go to the plug-ins page, get the jar with some general Tritonus utility stuff (tritonus_share-0.3.6.jar in my case) and get two jars for the MP3 decoder (javalayer.jar and tritonus_mp3-0.3.6.jar in my case). Put these three jars in you CLASSPATH and you should be set.
  • The people at JavaZoom also maintain some open source audio processing stuff, such as MP3SPI, which provides MP3 support for Java Sound. Three jars are required here: mp3spi1.9.4.jar from the MP3SPI download, jl1.0.1.jar from the JLayer download and the Tritonus utility jar (tritonus_share-0.3.6.jar in my case) from the Tritonus website. Putting these three in my CLASSPATH did the trick.

I haven't really got into comparing these options, so I can't say which is the best, but at least having three options is better than none.