Link to home
Start Free TrialLog in
Avatar of AndyBeckwith
AndyBeckwith

asked on

reading mp3 id3 tags in java...

I am trying to figure out how to read the id3 tag of an mp3 file with java. I think it is a fixed width thing at the end of the file... so is there a way to open a file and get to the end and work backwards or something?
Avatar of Droby10
Droby10

sure...it's been awhile, i'm going off the top of my head, and i don't have a reference available...so this may be a little off.

import java.io.*;

public static void main ( String args[] ) {
  try {
    File f = new File ( args[0] );
    FileInputStream fis = new FileInputStream ( f );
    int flen = fis.available();
    byte sig[] = new byte [ 3 ];
    byte artist[] = new byte [ 30 ];
    byte title[] = new byte [ 30 ];
    byte album[] = new byte [30];
    byte genre[] = new byte [1];
    byte comments = new byte [30];

    int id3len = sig.length + artist.length + title.length + album.length + genre.length + comment.length;

    fis.skip ( len - id3len );    
    fis.read ( sig );
    fis.read ( artist );
    fis.read ( title );
    fis.read ( album );
    fis.read ( genre );
    fis.read ( comments );
  } catch ( Exception e ) {
    e.printStackTrace();
  }
}


also, this code is for id3 version 1 only...which isn't used as much anymore...

id3 version 2 allowed title-streaming, embeded content...etc, and is much more robust...it's also much more difficult to filter as its embeded within the frames of an mp3 document.

just realized how ugly the code was...be sure to check verify after all of the reads...and close the inputstream at the end.
Avatar of AndyBeckwith

ASKER

do you have any info on the v2 tags?
no...but here's the definitive resource..

http://www.id3.org/
http://www.id3.org/develop.html
http://www.id3.org/id3v2.4.0-structure.txt

you'll also find links for source documentation at

http://id3lib.sourceforge.net/

-hope this helps.
and actually here's some java source...

http://www.rabbitfarm.com/site/id3.html

Back to your original code...

This is what I did, and it still wont work...

~~~ code ~~~
import java.io.*;
public class ReadId3 {

  public static void main ( String args[] ) {
    try {
      File f = new File ( args[0] );
      FileInputStream fis = new FileInputStream ( f );
      int flen = fis.available();
      byte [] sig = new byte [ 3 ];
      byte artist[] = new byte [ 30 ];
      byte [] title = new byte [ 30 ];
      byte album[] = new byte [30];
      byte genre[] = new byte [1];
      byte comments[] = new byte [30];

      int id3len = 124;

      fis.skip ( flen - id3len );    
      fis.read ( sig );
      fis.read ( artist );
      fis.read ( title );
      fis.read ( album );
      fis.read ( genre );
      fis.read ( comments );
    } catch ( Exception e ) {
      e.printStackTrace();
    }

    for( int i = 0; i < 30; i++ ){
      System.out.print( "" + title[ i ] );
    }
     
  }
}
~~~ end code ~~~

I still get this error:

~~~ error ~~~
ReadId3.java:31: cannot resolve symbol
symbol  : variable title
location: class ReadId3
        System.out.print( "" + title[ i ] );
                               ^
1 error
~~~ end error ~~~

I cant seem to force it to a string...
ASKER CERTIFIED SOLUTION
Avatar of Droby10
Droby10

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  Points to Droby10


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.