首先需要知道的是,MP3文件的文件信息都放在文件最后的128个字节里面,这128个字节分别存储的信息如下:
char Header[3]; /*标签头必须是"TAG"否则认为没有标签 */
char Title[30]; /*标题 */
char Artist[30]; /*作者 */
char Album[30]; /*专集 */
char Year[4]; /*出品年代 */
char Comment[28]; /*备注 */
char reserve; /*保留 */
char track;; /*音轨 */
char Genre; /*类型 */
代码:
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[1024];
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
/*---读取MP3文件尾部信息,并显示----*/
long size = file.length();
System.out.println("文件总字节数:"+size);
fis.skip(size-128);
//标志位TAG:3 byte
fis.read(buf,0,3);
String tag = new String(buf,0,3);
System.out.println( "ID3V1: "+tag);
//歌曲名称 30 byte
fis.read(buf,0,30);
String songname = new String(buf,0,30);
System.out.println( "song name: "+songname);
//歌手名称 30 byte
int len = fis.read(buf,0,30);
String songername = new String(buf,0,len);
System.out.println( "songer name: "+songername);
//专辑名称 30 byte
len = fis.read(buf,0,30);
String albumname = new String(buf,0,len);
System.out.println( "album name: "+albumname);
//年代 4 byte
fis.read(buf,0,4);
String year = new String(buf,0,4);
System.out.println( "year : "+year);
//comment 30 byte
fis.read(buf,0,28);
len = fis.read(buf,0,28);
String con = new String(buf,0,len);
System.out.println( "comment: "+con);
//genre 1 byte
fis.read(buf,0,1);
System.out.println( "Genre: "+buf[0]);
fis.close();
}
}
我读取的文件位于与src平行目录的images下。
运行结果为:
文件总字节数:4291383
ID3V1: TAG
song name: 吻别
注意:有些MP3文件并没有严格按照ID3V1的数据结构来存储信息,所以,有可能只能读取到部分信息。可以用UltraEdit打开MP3文件来查看相信的存储信息。