Android/Java 读、写MP3文件ID3V1信息

小编 2026-06-11 阅读:1542 评论:0
MP3的歌曲信息一般分两个大版本,分别是ID3V1和ID3V2,其中V2又分为好几个版本,具体百度一下,下方的代码仅仅是支持ID3V1。 需要用到的一个辅助工具(juniversalchardet)用于解...

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

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表