How to retrieve MP3 properties like album artist size in java

Written by david weinberg
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to retrieve MP3 properties like album artist size in java
(Jupiterimages/Comstock/Getty Images)

Java is a powerful programming language, but it lacks native support for playing MP3 files or for reading their properties. An external library can allow you to read metadata, such as artist name, album title, song title and song length, from an MP3 file into your program. Using any of these libraries is as simple as adding the library to your project, directing it to the file you would like to read information from and transferring the information into your program.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Java IDE

Show MoreHide

Instructions

    Using the MyID3 Library

  1. 1

    Add the MyID3 library to your current project (see Resources).

  2. 2

    Import necessary files into the class where you will be retrieving MP3 properties with the following lines of code:

    import java.io.*;

    import org.cmc.music.myid3.*;

    import org.cmc.music.common.MusicMetadata;

    These lines should be inserted at the beginning of the file, outside of the class declaration.

  3. 3

    Create a new File object pointing to the MP3 file from which you would like to read information. For example, the code

    File src = new File("C:/Users/59th Street Bridge Song.mp3");

    would create a File object pointing to an MP3 called "59th Street Bridge Song" in the "C:/Users" directory.

  4. 4

    Create a MusicMetadata object from the file object with the following code:

    MusicMetadata data = new MyID3().read(src).get Simplified();

  5. 5

    Read metadata into String objects using the access methods get Artist(), get Album() and getSongTitle(). For example, you could read the artist name into a String variable called "artist" with the following code:

    String artist = metadata.getArtist();

  6. 6

    Retrieve the length of the song in seconds with the following code:

    int length = metaData.getDurationSeconds().intValue();

    Using the JID3 Library

  1. 1

    Add the JID3 library to your current project (see Resources).

  2. 2
  3. 3

    Create a new File object pointing to the MP3 file from which you would like to read information. For example, the code

    File src = new File("C:/Users/59th Street Bridge Song.mp3");

    would create a File object pointing to an MP3 called "59th Street Bridge Song" in the "C:/Users" directory.

  4. 4

    Create a ID3V1Tag object from the file object with the following code:

    ID3V1Tag tag = new MP3File(src).getID3V1Tag();

  5. 5

    Read metadata into String objects using the access methods get Artist(), get Album() and get Title(). For example, you could read the artist name into a String variable called "artist" with the following code:

    String artist = tag.getArtist();

    Using the Java ID3 Tag Library

  1. 1

    Add the Java ID3 Tag Library to your current project (see Resources).

  2. 2

    Import necessary files into the class where you will be retrieving MP3 properties with the following lines of code:

    import java.io.*;

    import org.farng.mp3.*;

    These lines should be inserted at the beginning of the file, outside of the class declaration.

  3. 3

    Create a new File object pointing to the MP3 file from which you would like to read information. For example, the code

    File src = new File("C:/Users/59th Street Bridge Song.mp3");

    would create a File object pointing to an MP3 called "59th Street Bridge Song" in the "C:/Users" directory.

  4. 4

    Create a ID3 tag object from the file object with the following code:

    ID3v1 tag = new MP3File(src).getID3v1Tag();

  5. 5

    Read metadata into String objects using the access methods get Artist(), getAlbumTitle() and getSongTitle(). For example, you could read the artist name into a String variable called "artist" with the following code:

    String artist = tag.getArtist();

Tips and warnings

  • Note that only the MyID3 Library has methods for acquiring the length of a track.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.