Howto: Write MP3 Lyrics Tags With Jaudiotagger
Digital audio files like MP3 do more and more replace classic audio mediums like compact discs or cassettes. With recent multimedia players like the SAMSUNG Galaxy S it is now possible to host a complete audio collection including music and radio plays on one small and portable device.
With audio tags like the ID3 tag format for MP3 files additional pieces of information like album title, band name, or even the complete song lyrics may be added easily to the audio data. For the Java world Jaudiotagger is a widely used library capable of writing most versions and variants of audio file tags.
As many open source libraries also Jaudiotagger lacks necessary documentation and examples for software developers. This article tries to fill a gap here by showing how to write MP3 lyrics tags with Jaudiotagger properly. Properly here means that also special characters like German umlauts are displayed properly on handheld devices like the SAMSUNG Galaxy S.
The Java example step by step:
Let's take a closer look at the example class implementation. The first thing to do is to prepare some tag data to add to the audio file. The parameters used by the example (format, encoding, MP3 file, and text file) are specified by the main() method of the class. In order to run the example class please put a recent Jaudiotagger JAR library on your classpath. We used version 2.2.2 for development.
Please see the complete example code for ExampleLyrics.java.
Prepare some audio tag data (see ExampleLyrics.java)
As you can see a simple container with fields and values is being created first with some pieces of information like album name and artist. Then the song text aka lyrics is loaded from a simple text file. Please keep in mind that the fields have NOT been added to the MP3 file at this stage. Finally the target audio file is being opened for writing.
Set the lyrics tag encoding (see ExampleLyrics.java)
Now comes the tricky part: setting the encoding so that special characters like German umlauts are being displayed properly on a handheld device like SAMSUNG Galaxy S later. Assumed that the format be "ID3V23_TAG" the first "if" branch is executed. The desired encoding must be set by calling the setId3v23DefaultTextEncoding() method BEFORE the tag is instantiated. We commented out the exceptions raised by the API if the setEncoding() method would have been called instead as it may come to one's mind intuitively (so did we).
Add tag fields and write MP3 file (see ExampleLyrics.java)
In the final step the fields created before are written to the opened audio file. Optionally you could add additional pieces of information like album cover images here. This code secion has been commented out since it's not the main focus of this article. The interesting part here is that for cover artwork a different encoding must be used as for the lyrics. As you can see the ISO-8859-1 charset is activated before the artwork is added to the audio file. Please keep that in mind if you plan to add both textual fields and artwork together in one MP3 tag.
How to continue
- Download the full example code for ExampleLyrics.java
- Howto: VFSLib - VFS Reloaded
- Howto: Receiving Faxes With efax