Parse MP3 files using Java

Source: Internet
Author: User

First, you need to know that all the file information of the MP3 file is placed in the last 128 bytes of the file. The information stored in these 128 bytes is as follows:

Char header [3];/* the label header must be "tag"; otherwise, no label is considered */
Char title [30];/* Title */
Char artist [30];/* Author */
Char album [30];/* Special set */
Char year [4];/* production date */
Char comment [28];/* remarks */
Char reserve;/* retain */
Char track;/* audio track */
Char Genre;/* type */

 

Code:

Public class readmp3 {

 

/**

* @ Param ARGs

* @ Throws exception

*/

Public static void main (string [] ARGs) throws exception {

// Todo auto-generated method stub

 

String Path = system. getproperty ("user. dir") + "/images/wenbie.mp3 ";

Readmp3id3v1 (PATH );

}

 

Public static void readmp3id3v1 (string path) throws exception {

Byte [] Buf = new byte [1, 1024];

File file = new file (PATH );

 

Fileinputstream FCM = new fileinputstream (File );

/* --- Read the end information of the MP3 file and display ----*/

Long size = file. Length ();

System. Out. println ("total file Bytes:" + size );

 

FCM. Skip (size-128 );

 

// Tag: 3 byte

FS. Read (BUF );

String tag = new string (BUF, 0, 3 );

System. Out. println ("id3v1:" + tag );

 

// Song name 30 byte

FS. Read (BUF );

String songname = new string (BUF, 0, 30 );

System. Out. println ("song name:" + songname );

 

// Singer name 30 byte

Int Len = FCM. Read (BUF );

String songername = new string (BUF, 0, Len );

System. Out. println ("songer name:" + songername );

 

// Album name 30 byte

Len = FCM. Read (BUF );

String albumname = new string (BUF, 0, Len );

System. Out. println ("album name:" + albumname );

 

// 4 byte

FS. Read (BUF );

String year = new string (BUF, 0, 4 );

System. Out. println ("year:" + year );

 

// Comment 30 byte

FCM. Read (BUF );

Len = FCM. Read (BUF );

String con = new string (BUF, 0, Len );

System. Out. println ("comment:" + con );

// Genre 1 byte

FS. Read (BUF, 0, 1 );

System. Out. println ("genre:" + Buf [0]);

FCM. Close ();

 

}

}

The file I read is located in the images directory parallel to SRC.

The running result is:

Total file size: 4291383 bytes

Id3v1: Tag

Song name: Kiss goodbye

 

Note: Some MP3 files do not store information strictly according to the id3v1 data structure. Therefore, you may only be able to read part of the information. You can use ultraedit to open an MP3 file to view the storage information you believe.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.