java更新mp3文件信息的问题
想写个程序,去更新mp3文件的作者,专辑名等信息。百度了下,说是可以通过randaccessfile来更新文件最后的128个字节来更新。但是更新后,通过右键属性的方式,似乎并没有被修改,但是文件的确是修改了,重新读出来已经是更新后的内容了,请高手看看,这是什么问题。
public class Mp3BasicInfoBean {
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 Mp3BasicInfoBean(byte[] data) {
if (data.length != 128) {
throw new RuntimeException("数据长度不合法:" + data.length);
}
String tag = new String(data, 0, 3);
// 只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("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 Mp3BasicInfoBean() {
}
/**
* 返回是否合法
*
* @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;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Mp3InfoReader {
private Mp3BasicInfoBean info = null;
private RandomAccessFile ran = null;
private File file = null;
public Mp3InfoReader(String file) throws FileNotFoundException,IOException {
this.file = new File(file);
RandomAccessFile ran = new RandomAccessFile(file, "r");
ran.seek(ran.length() - 128);
byte[] buffer = new byte[128];
ran.read(buffer);
info = new Mp3BasicInfoBean(buffer);
ran.close();
}
public boolean updateInfoBean()throws IOException{
boolean result = false;
RandomAccessFile ran = new RandomAccessFile(file, "rw");
ran.seek(ran.length());
ran.write(info.getBytes());
ran.close();
return result;
}
public void setArtist(String authorName) {
this.info.setArtist(authorName);
}
public void setAlbum(String specialName) {
this.info.setAlbum(specialName);
}
public void setYear(String year) {
this.info.setYear(year);
}
public static void main(String[] args) throws IOException {
Mp3InfoReader read = new Mp3InfoReader("K:/a.mp3");
read.echo();
read.setAlbum("测试专辑");
read.setArtist( "测试作者");
read.setYear( "2005");
read.echo();
read.updateInfoBean();
}
public void echo(){
System.out.println("name:" + info.getSongName() + " year:"
+ info.getYear() + " 歌手:" + info.getArtist() + " 專輯名:"
+ info.getAlbum() + " 備注:" + info.getComment());
}
} java mp3 tag update --------------------编程问答-------------------- 顶一下,沉的好快 --------------------编程问答-------------------- 文件最后的128个字节是ID3v1,而windows会优先读取ID3v2,你找一个先读ID3v1的音乐播放器试一试
补充:Java , Java SE