java 音乐id3_Android/Java 读、写MP3文件ID3V1信息

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值