Solved

reading mp3 id3 tags in java...

Posted on 2001-06-04
8
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

724 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