12 Replies - 4068 Views - Last Post: 09 June 2008 - 11:37 AM Rate Topic: -----

#1 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Reading information about MP3's

Post icon  Posted 03 June 2008 - 01:26 PM

I know I'm not suppose to ask qeustions without script, but im trying to make a "MP3 player" on Java. My program works, but I realy want to give the user info on each song, like the name, time, album etc. I can't find anything 'bout this in the Java help files and extra manuels I have. Last time I tried to get some info, this site was the only one that helped me. can any one plz help me with finding the above info off a mp3 file??

Is This A Good Question/Topic? 0
  • +

Replies To: Reading information about MP3's

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading information about MP3's

Posted 03 June 2008 - 01:28 PM

View PostServant, on 3 Jun, 2008 - 01:26 PM, said:

I know I'm not suppose to ask qeustions without script, but im trying to make a "MP3 player" on Java. My program works, but I realy want to give the user info on each song, like the name, time, album etc. I can't find anything 'bout this in the Java help files and extra manuels I have. Last time I tried to get some info, this site was the only one that helped me. can any one plz help me with finding the above info off a mp3 file??

Do you have the Info or you really want to extract it from the MP3 file ?
If you have the info it can be easy.
Show us the piece of code where you read the .mp3 file

This post has been edited by pbl: 03 June 2008 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Reading information about MP3's

Posted 03 June 2008 - 01:58 PM

FileSoek (String naam)
{
File musiek = new File (naam);

File alfa[] = musiek.listFiles ();
for (int z = 0 ; z < alfa.length ; z++)
{
if (alfa [z].isFile ())
{

Files.add (alfa [z]);
// System.out.println (Files.lastElement ());
}
else if (alfa [z].isDirectory ())
{
FileSoek zxc = new FileSoek (alfa [z].toString ());
//System.out.println (alfa [z].toString ());
} //
}

}

this is where i get my directrys from. you see that i cant take the simple rout
Was This Post Helpful? 0
  • +
  • -

#4 cutegrrl  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 77
  • Joined: 12-May 08

Re: Reading information about MP3's

Posted 03 June 2008 - 05:08 PM

import java.io.*; 
 
public class Mp3 { 
    public static void main(String[] args) { 
        try { 
            File song = new File(args[0]); 
            FileInputStream file = new FileInputStream(song); 
            int size = (int)song.length(); 
            file.skip(size - 128); 
            byte[] last128 = new byte[128]; 
            file.read(last128); 
            String id3 = new String(last128); 
            String tag = id3.substring(0, 3); 
            if (tag.equals("TAG")) { 
                System.out.println("Title: " + id3.substring(3, 32)); 
                System.out.println("Artist: " + id3.substring(33, 62)); 
                System.out.println("Album: " + id3.substring(63, 91)); 
                System.out.println("Year: " + id3.substring(93, 97)); 
            } else 
                System.out.println(args[0] + " does not contain" 
                     + " ID3 info.");  
            file.close(); 
        } catch (Exception e) { 
            System.out.println("Error — " + e.toString()); 
        } 
    } 
}


This post has been edited by cutegrrl: 03 June 2008 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Reading information about MP3's

Posted 04 June 2008 - 02:04 AM

thanx. this realy helped a lot. got almost everything I need. One more question though, how would you get the track lenght? Anybody know?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading information about MP3's

Posted 04 June 2008 - 10:06 AM

View PostServant, on 4 Jun, 2008 - 02:04 AM, said:

thanx. this realy helped a lot. got almost everything I need. One more question though, how would you get the track lenght? Anybody know?


The size you got minus 128 may give you a good approximation

int size = (int)song.length();   



I don't know if there is a strict relation between number of bytes and number of seconds
Was This Post Helpful? 0
  • +
  • -

#7 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Reading information about MP3's

Posted 04 June 2008 - 10:30 AM

View Postpbl, on 4 Jun, 2008 - 07:06 PM, said:

I don't know if there is a strict relation between number of bytes and number of seconds

For a fixed bit rate mp3 there is definitely a strict relation. For VBRs I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#8 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Reading information about MP3's

Posted 04 June 2008 - 12:46 PM

I tried. if there is a relation between number of bytes and the time I couldnt find it. Thanks in any way. Any other idea's?
Was This Post Helpful? 1
  • +
  • -

#9 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Reading information about MP3's

Posted 04 June 2008 - 12:50 PM

Size in bytes / bitrate = length in seconds


Or the most precise calculating can be distilled from here
Was This Post Helpful? 0
  • +
  • -

#10 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Reading information about MP3's

Posted 05 June 2008 - 04:03 AM

View Post1lacca, on 4 Jun, 2008 - 12:50 PM, said:

Or the most precise calculating can be distilled from here


ok, I checked. The problem is I now dont know how to extract the bitrate from the mp3. not only that, but I learned that the previous anser (ID3 TAG) will only give me the information on a third of my mp3's. seems the new mp3's have a tag in the beginning, but I cant seem to find it. :'(
Any other idea's?
Was This Post Helpful? 0
  • +
  • -

#11 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Reading information about MP3's

Posted 05 June 2008 - 04:15 AM

View PostServant, on 5 Jun, 2008 - 01:03 PM, said:

ok, I checked. The problem is I now dont know how to extract the bitrate from the mp3.

Then read it again, it is there. It is not about ID3 tag, it is about the MP3 itself, so it will work with VBRs as well (but you'll have to read through the whole file)

View PostServant, on 5 Jun, 2008 - 01:03 PM, said:

not only that, but I learned that the previous anser (ID3 TAG) will only give me the information on a third of my mp3's. seems the new mp3's have a tag in the beginning, but I cant seem to find it. :'(

There are two formats, ID3v2 is also used.
Other than that, if an mp3 is not tagged, then you won't be able to extract the title of the song. Some program use some heuristic in this case, and try to parse the filename with different strategies, or simply display it without the extension.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Reading information about MP3's

Posted 05 June 2008 - 08:15 AM

The first ID3 format is pretty basic, as you've seen; just chop off the last 128 bytes. There's another variant of it with track info that just takes another couple bytes from the comments, more here: http://www.id3.org/ID3v1

ID3v2, in comparison, is dreadfully complex. There are known fields, and user fields, no fixed sizes, unicode... great fun. You'd honestly be better off looking at one of the many public implementations first. Try here: http://www.id3.org/Implementations

If you do an ID3v2 dump of a file you can find all kinds of interesting stuff. Lots of MP3 players throw their own references into the things. Also, many players are in the habit of double tagging, with both versions logged on the file at the same time. It's not uncommon to see files with tagging cruft at the end because various programs didn't play nice with what was already there.
Was This Post Helpful? 0
  • +
  • -

#13 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Reading information about MP3's

Posted 09 June 2008 - 11:37 AM

OK. I've made a lot of compromises. I'm getting the names, that I cant get from the TAG methode, by just taking the filename. Unfortenately though I need each song's length(sec). Does any one have an idea of how to get either the song length or its bitrate. (I have to read it from the mp3 file self.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1