Solved

reading mp3 id3 tags in java...

Posted on 2001-06-04
8
457 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:AndyBeckwith
  • 5
  • 2
8 Comments
 
LVL 5

Expert Comment

by:Droby10
ID: 6154612
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.

0
 
LVL 5

Expert Comment

by:Droby10
ID: 6154619
just realized how ugly the code was...be sure to check verify after all of the reads...and close the inputstream at the end.
0
 

Author Comment

by:AndyBeckwith
ID: 6154690
do you have any info on the v2 tags?
0
 
LVL 5

Expert Comment

by:Droby10
ID: 6154714
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Droby10
ID: 6154718
and actually here's some java source...

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

0
 

Author Comment

by:AndyBeckwith
ID: 6155273
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...
0
 
LVL 5

Accepted Solution

by:
Droby10 earned 100 total points
ID: 6155911
your print statement is outside of the try loop where title is defined...title is out-of-scope...if you predeclare it, it should work.

import java.io.*;
public class ReadId3 {

 public static void main ( String args[] ) {
   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];
   try {
     File f = new File ( args[0] );
     FileInputStream fis = new FileInputStream ( f );
     int flen = fis.available();
     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 ] );
   }
   
 }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8898913


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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
Hibernate Not Rolling Back When something fails in a stored procedure 2 52
Unable to start eclipse ? 17 132
Java and GPO 11 71
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now