在编写播放器的时候,需要读取mp3里包含的信息,于是写了下面的代码以读取ID3V2及ID3V1的信息,最终是数据保存在了info中 ,并且图片保存在了mp3文件同目录下。 需要注意的是,读取ID3V1信息的时候不知道它是用的什么编码,可能会导致读取的信息成为乱码。 如果有问题可以私聊我,若要使用直接复制粘贴即可 例:MediaInfo mediaInfo = new MediaInfo(file, id); 如果用不上id随意设置一个int类型数值即可,如 0; 编写不易,请多多点赞收藏 、打赏。 后续会编写读取falc格式的信息。
public class MediaInfo implements Serializable {
private int viewType = 0;
private HashMap info = new HashMap<String, String>();
private int id;
private byte[] byteArray = null;
byte[] imageByte = null;
File file = null;
public MediaInfo(File file, int id){
this.file = file;
this.id = id;
info.put("PATH", file.getAbsolutePath());
getID3V2(this.file);
}
public void setViewType(int viewType) {
this.viewType = viewType;
}
public int getViewType() {
return viewType;
}
public String getFilePath() { return String.valueOf(info.get("PATH"));}
public String getImagePath() { return String.valueOf(info.get("APIC"));}
public String getTitle(){
return String.valueOf(info.get("TIT2"));
}
public String getAuthor(){
return String.valueOf(info.get("TPE1"));
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public byte[] saveImageByte(byte[] byteArray, int offset, int length){
byte[] imageByte = new byte[length];
for(int i = 0; i < length; i++){
imageByte[i] = byteArray[offset + i];
}
return imageByte;
}
public int scanID3V2(int fileFramSize, String target) {
int framsize = 0;
String charset = Charset.defaultCharset().name();
fileFramSize += 4;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
framsize = Byte.toUnsignedInt(byteArray[fileFramSize]) * 0x1000000
+ Byte.toUnsignedInt(byteArray[fileFramSize + 1]) * 0x10000
+ Byte.toUnsignedInt(byteArray[fileFramSize + 2]) * 0x100
+ Byte.toUnsignedInt(byteArray[fileFramSize + 3]);
}
fileFramSize += 6;
try {
if (Byte.toString(byteArray[fileFramSize]).equals("0")) {
charset = "ISO-8859-1";
} else if (Byte.toString(byteArray[fileFramSize]).equals("1")) {
charset = "UTF-16";
} else if (Byte.toString(byteArray[fileFramSize]).equals("2")) {
charset = "UTF-16BE";
} else if (Byte.toString(byteArray[fileFramSize]).equals("3")) {
charset = "UTF-8";
}
if(target.equals("APIC")){
getImageByte_ID3V2(new String(byteArray, fileFramSize + 1, 25, charset), fileFramSize, framsize);
}else {
info.put(target, new String(byteArray, fileFramSize + 1, framsize - 1, charset));;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
fileFramSize += framsize;
charset = null;
if (fileFramSize > (byteArray.length / 2) && info.containsKey("TIT2") && info.containsKey("TPE1")){return -1;}
return fileFramSize;
}
public byte[] getTargetByte(byte[] imageByte, byte[] startByte, byte[] endByte){
int startIndex = -1, endIndex = -1;
byte[] targetByte = null;
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;}
}
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;}
}
if(endIndex != -1){
break;
}
}
}else{ return null; }
if(endIndex != -1 && endIndex > startIndex){
targetByte = new byte[endIndex - startIndex + 1];
for(int i = 0; i < targetByte.length; i++) {
targetByte[i] = imageByte[startIndex + i];
}
}else{ return null;}
return targetByte;
}
public void createImage(){
File imageFile = new File(file.getParent() + "/musicImage");
char[] temp = getAuthor().toCharArray();
if(!imageFile.exists()){
imageFile.mkdir();
}
for(int i = 0, count = 0; i < temp.length; i++){
if(temp[i] == '/'){
if(++count == 3){
this.info.put("TPE1", String.valueOf(temp,0, i) + "……");
}
}
}
temp = null;
Log.e("temp", getAuthor());
imageFile = new File(file.getParent() + "/musicImage/" + getAuthor().replace("/","、")
+ "_" + getTitle().replace("/","、") + "." + this.info.get("APIC"));
FileOutputStream fileOutputStream = null;
if(imageFile.exists() == false){
try {
imageFile.createNewFile();
fileOutputStream = new FileOutputStream(imageFile);
fileOutputStream.write(this.imageByte);
fileOutputStream.close();
fileOutputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
info.put("APIC",imageFile.getAbsolutePath());
this.imageByte = null;
imageFile = null;
}
public void getImageByte_ID3V2(String imageInfo, int fileFramSize, int framsize){
Log.e("imageInfo", ""+ imageInfo+"\n");
if(imageInfo.toLowerCase().indexOf("jfif", 11) != -1
|| imageInfo.toLowerCase().indexOf("jpg", 11) != -1
|| imageInfo.toLowerCase().indexOf("jpeg", 11) != -1){
byte[] JPGStartByte = new byte[] {(byte) 0xFF,(byte) 0xD8,(byte) 0xFF};
byte[] JPGEndByte = new byte[] {(byte) 0xFF,(byte) 0xD9};
this.info.put("APIC", "jpg");
this.imageByte = getTargetByte(saveImageByte(this.byteArray,fileFramSize, framsize), JPGStartByte, JPGEndByte);
}else if(imageInfo.toLowerCase().indexOf("png", 11) != -1){
byte[] PNGStartByte = new byte[] {(byte) 0x89,(byte) 0x50,(byte) 0x4E,(byte) 0x47};
byte[] PNGEndByte = new byte[] {(byte) 0xAE,(byte) 0x42,(byte) 0x60,(byte) 0x82};
this.info.put("APIC", "png");
this.imageByte = getTargetByte(saveImageByte(this.byteArray,fileFramSize, framsize), PNGStartByte, PNGEndByte);
}else if(imageInfo.toLowerCase().indexOf("gif", 11) != -1){
byte[] GIFStartByte = new byte[] { (byte) 0x47,(byte) 0x49,(byte) 0x46,(byte) 0x38};
byte[] GIFEndByte = new byte[] { (byte) 0x00,(byte) 0x3B};
this.info.put("APIC", "gif");
this.imageByte = getTargetByte(saveImageByte(this.byteArray,fileFramSize, framsize), GIFStartByte, GIFEndByte);
}
}
public void getID3V2(File file){
this.byteArray = new byte[10];
int fileFramSize = 0;
String framHead = null;
try(RandomAccessFile accessFile = new RandomAccessFile(file,"r")) {
accessFile.seek(0);
accessFile.read(byteArray);
if(new String(byteArray,0,3).equals("ID3") && Byte.toString(byteArray[3]).equals("3")){
fileFramSize = (byteArray[6]&0x7F)*(0x200000)+(byteArray[7]&0x7F)*(0x4000)
+(byteArray[8]&0x7F)*(0x80) + (byteArray[9]&(0x7F));
byteArray = new byte[fileFramSize];
accessFile.read(byteArray);
fileFramSize = 0;
while(fileFramSize < byteArray.length - 10){
framHead = new String(byteArray,fileFramSize,4);
fileFramSize = scanID3V2(fileFramSize, framHead);
if(fileFramSize == -1){break;}
}
createImage();
}else {
getID3V1(file);
}
} catch (IOException e) {
e.printStackTrace();
getID3V1(file);
}
framHead = null;
this.file = null;
this.byteArray = null;
}
public void getID3V1(File file){
byteArray = new byte[128];
try(RandomAccessFile accessFile = new RandomAccessFile(file,"r")) {
accessFile.seek(accessFile.length() - 128);
if(!"TAG".equalsIgnoreCase(new String(byteArray, 0, 3))){
info.put("标题", "未知");
info.put("作者", "未知");
info.put("专辑", "未知");
info.put("年份", "未知");
}
else{
info.put("标题", new String(byteArray, 3, 30));
info.put("作者", new String(byteArray, 33, 30));
info.put("专辑", new String(byteArray, 63, 30));
info.put("年份", new String(byteArray, 93, 4));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
|