java深度解析mp3文件

mp3这种音乐格式的音乐文件在我们的生活中遇到的是最多最普通的,那么他们有那些属性了。要是你看到下面的解释没有觉得他很强大,那你就是一个强人,在网上淘的,加上自己的一些理解吧

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
 
/**
 *
 * 这个类用来获取一首音乐的详细信息
 * @author pengqinping
 */
public class Mp3
{
   
    private RandomAccessFile ran = null;
   
    private static File file = null;
   
    /**
     * @param file(我们创建当前类的对象的时候初始化我们传过来的music对象)
     * @throws FileNotFoundException
     */
    public Mp3(File file) throws FileNotFoundException
    {
        super();
        Mp3.setFile(file);
        System.out.println(file.length() + "字节");
        System.out.println(((double) file.length()) / (1024 * 1024));
        ran = new RandomAccessFile(file, "r");
        System.out.println("文件装载完毕");
    }
   
    /**
     *
     * 获取音乐的详细信息并且保存在map中
     * @param file
     * @return 返回类型说明
     */
    public static Map<String, String> getMusicMsg(File file)
    {
        Map<String, String> map = new HashMap<String, String>();
        try
        {
            Mp3 read = new Mp3(file);
            byte[] buffer = new byte[128];
            read.ran.seek(read.ran.length() - 128);
            read.ran.read(buffer);
            SongInfo info = new SongInfo(buffer);
            System.out.println("Name:"+info.getSongName()+";artist:"+info.getArtist()+";album:"+info.getAlbum());
            map.put("musicname", info.getSongName());
            map.put("musicauthor", info.getArtist());
            map.put("musicalbum", info.getAlbum());
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return map;
    }
   
    public static void setFile(File file)
    {
        Mp3.file = file;
    }
   
    public static File getFile()
    {
        return file;
    }
   
    public static void main(String[] args)
    {
        getMusicMsg(new File("../MP3/music2/Home And Dry.mp3"));
    }
   
   
   
}
 
 
package Mp3;
 
public class SongInfo {
 
private final String TAG = "TAG"; // 文件头1-3
private String songName; // 歌曲名4-33
private String artist; // 歌手名34-63
private String album; // 专辑名61-93
private String year; // 年94-97
private String comment; // 备注98-125
private byte r1, r2, r3; // 三个保留位126,127,128
private boolean valid; // 是否合法
public transient String fileName; // 此歌曲对应的文件名,没有封装
 
public SongInfo(byte[] data) {
if (data.length != 128) {
throw new RuntimeException("数据长度不合法:" + data.length);
}
String tag = new String(data, 0, 3);
// 只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("TAG")) {
    System.out.println("这首歌有TAG++"+tag);
valid = true;
songName = new String(data, 3, 30).trim();
artist = new String(data, 33, 30).trim();
album = new String(data, 63, 30).trim();
year = new String(data, 93, 4).trim();
comment = new String(data, 97, 28).trim();
r1 = data[125];
r2 = data[126];
r3 = data[127];
} else {
valid = false;
}
}
 
public SongInfo() {
}
 
/**
 * 返回是否合法
 *
 * @return 是否
 */
public boolean isValid() {
return valid;
}
 
/**
 * 得到此对象的128个字节的表示形式
 *
 * @return
 */
public byte[] getBytes() {
byte[] data = new byte[128];
System.arraycopy(TAG.getBytes(), 0, data, 0, 3);
byte[] temp = songName.getBytes();
System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length);
temp = artist.getBytes();
System.arraycopy(temp, 0, data, 33, temp.length > 30 ? 30 : temp.length);
temp = album.getBytes();
System.arraycopy(temp, 0, data, 63, temp.length > 30 ? 30 : temp.length);
temp = year.getBytes();
System.arraycopy(temp, 0, data, 93, temp.length > 4 ? 4 : temp.length);
temp = comment.getBytes();
System.arraycopy(temp, 0, data, 97, temp.length > 28 ? 28 : temp.length);
data[125] = r1;
data[126] = r2;
data[127] = r3;
return data;
}
 
public String getArtist() {
return artist;
}
 
public void setArtist(String authorName) {
this.artist = authorName;
}
 
public String getComment() {
return comment;
}
 
public void setComment(String comment) {
this.comment = comment;
}
 
public byte getR1() {
return r1;
}
 
public void setR1(byte r1) {
this.r1 = r1;
}
 
public byte getR2() {
return r2;
}
 
public void setR2(byte r2) {
this.r2 = r2;
}
 
public byte getR3() {
return r3;
}
 
public void setR3(byte r3) {
this.r3 = r3;
}
 
public String getSongName() {
return songName;
}
 
public void setSongName(String songName) {
if (songName == null) {
throw new NullPointerException("歌名不能是null!");
}
valid = true;
this.songName = songName;
}
 
public String getAlbum() {
return album;
}
 
public void setAlbum(String specialName) {
this.album = specialName;
}
 
public String getYear() {
return year;
}
 
public void setYear(String year) {
this.year = year;
}
 
}
posted @ 2012-06-13 00:46  pengqinping  阅读(2139)  评论(0编辑  收藏  举报