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);
}
2892






