MP3的歌曲信息一般分两个大版本,分别是ID3V1和ID3V2,其中V2又分为好几个版本,具体百度一下,下方的代码仅仅是支持ID3V1。
需要用到的一个辅助工具(juniversalchardet)用于解决乱码问题,具体看博客:https://my.oschina.net/u/1462828/blog/2877749
具体看代码:
/**
* 获取MP3文件信息
*
* @param path MP3文件对象
*/
public static MusicInfoV1Entity getMusicInfoV1(String path) {
if (path == null) {
return null;
}
return getMusicInfoV1(new File(path));
}
public static MusicInfoV1Entity getMusicInfoV1(File musicFile) {
if (musicFile == null) {
return null;
}
MusicInfoV1Entity v1Entity;
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(musicFile, "r");
byte[] buffer = new byte[128];
randomAccessFile.seek(randomAccessFile.length() - 128);
randomAccessFile.read(buffer);
if (buffer.length == 128) {
v1Entity = new MusicInfoV1Entity();
String tag = new String(buffer, 0, 3);
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(buffer, 0, buffer.length);
detector.dataEnd();
String charset = detector.getDetectedCharset();
detector.reset();
// 只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("TAG")) {
// 歌曲名
String songName = new String(buffer, 3, 30, charset).trim();
// 艺术家
String artist = new String(buffer, 33, 30, charset).trim();
// 所属唱片
String album = new String(buffer, 63, 30, charset).trim();
// 发行年
String year = new String(buffer, 93, 4, charset).trim();
// 备注
String comment = new String(buffer, 97, 28, charset).trim();
v1Entity.setTitle(songName);
v1Entity.setArtist(artist);
v1Entity.setAlbum(album);
v1Entity.setYear(year);
v1Entity.setComment(comment);
ALog.e("歌曲名:" + songName);
ALog.e("艺术家:" + artist);
ALog.e("所属唱片:" + album);
ALog.e("发行年:" + year);
ALog.e("备注:" + comment);
return v1Entity;
} else {
ALog.e("无效的歌曲信息...");
return null;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 写入mp3的ID3V1文件信息
*
* @param path
* @param v1Entity
*/
public static void setMusicInfoV1(String path, MusicInfoV1Entity v1Entity) {
try {
byte[] bufferAll = new byte[128];
byte[] buffTag;
byte[] buffSoundName = new byte[30];
byte[] buffArtist = new byte[30];
byte[] buffAlbum = new byte[30];
byte[] buffYear = new byte[4];
byte[] buffComment = new byte[28];
byte[] buffFoot;
buffTag = "TAG".getBytes();
byte[] cache;
if (v1Entity.getTitle() != null) {
cache = v1Entity.getTitle().getBytes("GBK");
System.arraycopy(cache, 0, buffSoundName, 0, cache.length);
}
if (v1Entity.getArtist() != null) {
cache = v1Entity.getArtist().getBytes("GBK");
System.arraycopy(cache, 0, buffArtist, 0, cache.length);
}
if (v1Entity.getAlbum() != null) {
try {
cache = v1Entity.getAlbum().getBytes("GBK");
System.arraycopy(cache, 0, buffAlbum, 0, cache.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
if (v1Entity.getYear() != null) {
cache = v1Entity.getYear().getBytes("GBK");
System.arraycopy(cache, 0, buffYear, 0, cache.length);
}
if (v1Entity.getComment() != null) {
cache = v1Entity.getComment().getBytes("GBK");
int num = 28;
if (cache.length <= num) {
num = cache.length;
}
System.arraycopy(cache, 0, buffComment, 0, num);
}
buffFoot = "111".getBytes();
System.arraycopy(buffTag, 0, bufferAll, 0, 3);
System.arraycopy(buffSoundName, 0, bufferAll, 3, 30);
System.arraycopy(buffArtist, 0, bufferAll, 33, 30);
System.arraycopy(buffAlbum, 0, bufferAll, 63, 30);
System.arraycopy(buffYear, 0, bufferAll, 93, 4);
System.arraycopy(buffComment, 0, bufferAll, 97, 28);
System.arraycopy(buffFoot, 0, bufferAll, 125, 3);
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(path), "rw");
long len = randomAccessFile.length();
if (getMusicInfoV1(path) != null) {
//有v1了,需要把后面的128删掉
len = randomAccessFile.length() - 128;
}
randomAccessFile.seek(len);
randomAccessFile.write(bufferAll, 0, bufferAll.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
附上实体类:
public class MusicInfoV1Entity {
//歌曲名字
String title;
// 艺术家
String artist;
// 作曲家(ID3V1不支持这个字段)
String composer;
// 所属唱片
String album;
// 发行年
String year;
// 备注
String comment;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getComposer() {
return composer;
}
public void setComposer(String composer) {
this.composer = composer;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
当然,鉴于各种操作比较复杂,所以还是习惯性拿来主义,
推荐使用一个叫Jaudiotagger的jar包,支持mp3/m4a/wav/flac等格式的音频信息读写。
官网地址:http://www.jthink.net/jaudiotagger/index.jsp