博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android/Java 读、写MP3文件ID3V1信息
阅读量:7108 次
发布时间:2019-06-28

本文共 6574 字,大约阅读时间需要 21 分钟。

hot3.png

MP3的歌曲信息一般分两个大版本,分别是ID3V1和ID3V2,其中V2又分为好几个版本,具体百度一下,下方的代码仅仅是支持ID3V1。

需要用到的一个辅助工具()用于解决乱码问题,具体看博客: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;    }}

 

当然,鉴于各种操作比较复杂,所以还是习惯性拿来主义,

推荐使用一个叫的jar包,支持mp3/m4a/wav/flac等格式的音频信息读写。

官网地址:http://www.jthink.net/jaudiotagger/index.jsp

转载于:https://my.oschina.net/u/1462828/blog/2878185

你可能感兴趣的文章
初识servlet
查看>>
WebClien简单示例(一)
查看>>
POJ-1472 Instant Complexity 模拟
查看>>
定位错误:glibc detected :double free or corruption (!prev): 0x08a03b88
查看>>
Linux信号列表
查看>>
11.11.24
查看>>
PHP面向对象之final关键字
查看>>
模块化编程
查看>>
关于visual studio 2010 与windows phone 的整合安装问题
查看>>
数据预处理规则
查看>>
为梦想,每天坚持30分钟
查看>>
振奋人心啊!!!!下一代.NET——ASP.NET vNext
查看>>
Windows 7 安装.net framework 4.0 失败,错误HRESULT 0xc8000222解决办法
查看>>
maven基础依赖外部lib包(依赖钉钉sdk为例)
查看>>
php7使用curl
查看>>
[ZJOI2010]贪吃的老鼠(网络流+建图)
查看>>
HTTPSQS(HTTP Simple Queue Service)消息队列
查看>>
Cortex-M3 入门指南(三):时钟总线与复位时钟控制器
查看>>
VBS 操作Excel
查看>>
3176:Cow Bowling
查看>>