leisenfels mit schrift und slogan 48

Howto: Write MP3 Lyrics Tags With Jaudiotagger

Android Audio Player

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 AudioTag Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
 // Let's prepare some audio tag data
 Hashtable<FieldKey, String> filefields = new Hashtable<FieldKey, String>(0);
 
 // Album data
 filefields.put(FieldKey.ALBUM, "Aenima");
 filefields.put(FieldKey.ALBUM_ARTIST, "Tool");
 
 // Title data
 filefields.put(FieldKey.TITLE, "Die Eier von Satan");
 filefields.put(FieldKey.ARTIST, "Tool");
 filefields.put(FieldKey.YEAR, "1996");
 filefields.put(FieldKey.TRACK, "10");
 
 // Load lyrics from text file
 String lyrics = new String(readTextData(textfile));
 filefields.put(FieldKey.LYRICS, lyrics);
 
 // Open audio file first, audio data is rewritten
 AudioFile audiofile = AudioFileIO.read(mp3file);
 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
 Tag tag;
 TextEncoding textencoding = TextEncoding.getInstanceOf();
 TagOptionSingleton tagoptions = TagOptionSingleton.getInstance();
 
 // jaudiotagger supports additional tag formats like MP4 or Vorbis.
 // We will concentrate on the relevant ID3V23 and ID3V24 versions.
 
 if (format.equals(SupportedTagFormat.ID3V23_TAG.name())) {
 
 // Set defined charset for lyrics including umlauts
 
 // Supported by ID3V23 (see TagOptionSingleton class):
 // TextEncoding.ISO_8859_1, TextEncoding.UTF_16
 tagoptions.setId3v23DefaultTextEncoding(textencoding.getIdForValue(encoding).byteValue());
 
 tag = new ID3v23Tag();
 
 /* The following does not work:
 
  tag.setEncoding(this.encoding);
 
  2014-11-27 09:33:31.187 WARNING (err) java.lang.UnsupportedOperationException: Not Implemented Yet
  2014-11-27 09:33:31.188 WARNING (err) at org.jaudiotagger.tag.id3.AbstractID3v2Tag.setEncoding(AbstractID3v2Tag.java:2105)
  2014-11-27 09:33:31.188 WARNING (err) at com.lf.pindexer.tagging.AudioTagger.run(AudioTagger.java:161)
  2014-11-27 09:33:31.188 WARNING (err) at java.lang.Thread.run(Thread.java:662)
  */
 }
 else { // Must be SupportedTagFormat.ID3V24_TAG
 
 // Supported by ID3V24 (see TagOptionSingleton class):
 // TextEncoding.ISO_8859_1, TextEncoding.UTF_16, TextEncoding.UTF_16BE, TextEncoding.UTF_8
 tagoptions.setId3v24DefaultTextEncoding(textencoding.getIdForValue(encoding).byteValue());
 
 tag = new ID3v24Tag();
 }
 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
 // Activate the new tag fields based on the encoding set before
 audiofile.setTag(tag);
 Enumeration<FieldKey> en = filefields.keys();
 while (en.hasMoreElements()) {
 FieldKey key = en.nextElement();
 tag.setField(key, filefields.get(key));
 }
 
 // One could add some artwork like a JPEG album cover here, too.
 // The encoding must then be changed again to be properly processed.
 
 // Set ISO-8859-1 charset for images, may be not shown by player with UTF-16
 /*
  byte isoencoding = textencoding.getIdForValue(TextEncoding.CHARSET_ISO_8859_1).byteValue();
  if (format.equals(SupportedTagFormat.ID3V23_TAG.name())) {
  tagoptions.setId3v23DefaultTextEncoding(isoencoding);
  }
  else { // Must be SupportedTagFormat.ID3V24_TAG
  tagoptions.setId3v24DefaultTextEncoding(isoencoding);
  }
  tag.setField(ArtworkFactory.createArtworkFromFile(new File("C:\\Temp\\cover.jpg")));
  */
 
 // Finally store new tag data into audio file
 AudioFileIO.write(audiofile);
 

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

Print Email