Solved

reading mp3 id3 tags in java...

Posted on 2001-06-04
8
461 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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