java 获取ID3V2中歌词与图片

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;


public class MediaInfoHandler {
    public static String LYRICS = null;
//    public static Bitmap APIC = null;
    String USLT, TEXT;
    File file;

    public MediaInfoHandler(File file) {
        this.file = file;
    }

    public void getID3V2() throws IOException {
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
            randomAccessFile.seek(0);
            byte[] headByte = new byte[10];
            randomAccessFile.read(headByte);
            if (new String(headByte, 0, 3).equals("ID3")) {
                /*fileFrameSize保存标签内容大小,不包含标签头大小(10 byte)*/
                int framesSize = (headByte[6] & 0x7F) * 0x200000 + (headByte[7] & 0x7F) * (0x4000) + (headByte[8] & 0x7F) * (0x80) + (headByte[9] & (0x7F));
                byte[] framesByte = new byte[framesSize];
                randomAccessFile.read(framesByte);
                /*fileFrameSize 重赋值为0记录当前读取位置*/
                int pointer = 0;
                while (pointer < framesByte.length) {
                    String frameID = new String(framesByte, pointer, 4);
                    pointer += 4;
                    int frameSize = (framesByte[pointer] & 0xff) * 0x1000000 + (framesByte[pointer + 1] & 0xff) * 0x10000 + (framesByte[pointer + 2] & 0xff) * 0x100 + (framesByte[pointer + 3] & 0xff);
                    pointer += 6;
                    String charset = Charset.defaultCharset().name();
                    if (framesByte[pointer] == 0) charset = "ISO-8859-1";
                    else if (framesByte[pointer] == 1) charset = "UTF-16";
                    if (framesByte[pointer + 1] == 0) break;
                    switch (frameID) {
                        case "TEXT":
                            TEXT = new String(framesByte, pointer + 1, frameSize - 1, "GBK");
                            break;
                        case "USLT":
                            USLT = new String(framesByte, pointer + 8, frameSize - 8, charset);
                            break;
//                        case "APIC":
//                            byte[] imageByte = new byte[frameSize];
//                            System.arraycopy(framesByte, pointer, imageByte, 0, frameSize);
//                            byte[] startByte = new byte[]{(byte) 0xFF, (byte) 0xD8, (byte) 0xFF};
//                            byte[] endByte = new byte[]{(byte) 0xFF, (byte) 0xD9};
//                            int startIndex = -1, endIndex = -1;
//                            byte[] targetByte = new byte[0];
//                            for (int i = 0; i < imageByte.length - startByte.length; i++) {
//                                for (int k = 0; k < startByte.length; k++) {
//                                    if (imageByte[i + k] != startByte[k]) break;
//                                    if (k == startByte.length - 1) {
//                                        startIndex = i;
//                                        break;
//                                    }
//                                }
//                                if (startIndex != -1) break;
//                            }
//                            if (startIndex != -1) {
//                                for (int i = imageByte.length - 1; i >= endByte.length - 1; i--) {
//                                    for (int k = 0; k < endByte.length; k++) {
//                                        if (imageByte[i - k] != endByte[endByte.length - 1 - k])
//                                            break;
//                                        if (k == endByte.length - 1) {
//                                            endIndex = i;
//                                            break;
//                                        }
//                                    }
//                                    if (endIndex != -1) break;
//                                }
//                            }
//                            if (endIndex != -1 && endIndex > startIndex) {
//                                targetByte = new byte[endIndex - startIndex + 1];
//                                System.arraycopy(imageByte, startIndex, targetByte, 0, targetByte.length);
//                            }
//                            APIC = BitmapFactory.decodeByteArray(targetByte, 0, targetByte.length);
//                            break;
                    }
                    pointer += frameSize;
                }
            }
            if (TEXT != null) LYRICS = TEXT;
            else if (USLT != null) LYRICS = USLT;
        }
    }
}

补充一下:

上面的有 TEXT 是因为酷我下载音乐文件嵌入歌词是存在 TEXT 中的。如果封面图片还是用下面的方法实在。

retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
byte[] coverBytes = retriever.getEmbeddedPicture();
if (coverBytes != null) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
    albumIV.setImageBitmap(bitmap);
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值