如何用Java读取MP3文件的Tag信息? 其中Tag信息可能是中文的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package mp3;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class ReadMp3 { private SongInfo info = null; private RandomAccessFile ran = null; private File file = null; public ReadMp3() throws FileNotFoundException { file = new File("./rec/感觉不到你.mp3"); ran = new RandomAccessFile(file, "r"); System.out.println("文件裝載完畢"); } public static void main(String[] args) throws IOException { ReadMp3 read = new ReadMp3(); byte[] buffer = new byte[128]; read.ran.seek(read.ran.length() - 128); read.ran.read(buffer); SongInfo info = new SongInfo(buffer); System.out.println("name:" + info.getSongName() + " year:" + info.getYear() + " 歌手:" + info.getArtist() + " 專輯名:" + info.getAlbum() + " 備注:" + info.getComment()); }}package mp3;/** * 一个歌曲信息的类的结构表示 这个歌曲是使用ID3V1的信息存储结构的 * * @author hadeslee */public class SongInfo { private final String TAG = "TAG"; // 文件头1-3 private String songName; // 歌曲名4-33 private String artist; // 歌手名34-63 private String album; // 专辑名61-93 private String year; // 年94-97 private String comment; // 备注98-125 private byte r1, r2, r3; // 三个保留位126,127,128 private boolean valid; // 是否合法 public transient String fileName; // 此歌曲对应的文件名,没有封装 public SongInfo(byte[] data) { if (data.length != 128) { throw new RuntimeException("数据长度不合法:" + data.length); } String tag = new String(data, 0, 3); // 只有前三个字节是TAG才处理后面的字节 if (tag.equalsIgnoreCase("TAG")) { valid = true; songName = new String(data, 3, 30).trim(); artist = new String(data, 33, 30).trim(); album = new String(data, 63, 30).trim(); year = new String(data, 93, 4).trim(); comment = new String(data, 97, 28).trim(); r1 = data[125]; r2 = data[126]; r3 = data[127]; } else { valid = false; } } public SongInfo() { } /** * 返回是否合法 * * @return 是否 */ public boolean isValid() { return valid; } /** * 得到此对象的128个字节的表示形式 * * @return */ public byte[] getBytes() { byte[] data = new byte[128]; System.arraycopy(TAG.getBytes(), 0, data, 0, 3); byte[] temp = songName.getBytes(); System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length); temp = artist.getBytes(); System .arraycopy(temp, 0, data, 33, temp.length > 30 ? 30 : temp.length); temp = album.getBytes(); System .arraycopy(temp, 0, data, 63, temp.length > 30 ? 30 : temp.length); temp = year.getBytes(); System.arraycopy(temp, 0, data, 93, temp.length > 4 ? 4 : temp.length); temp = comment.getBytes(); System .arraycopy(temp, 0, data, 97, temp.length > 28 ? 28 : temp.length); data[125] = r1; data[126] = r2; data[127] = r3; return data; } public String getArtist() { return artist; } public void setArtist(String authorName) { this.artist = authorName; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public byte getR1() { return r1; } public void setR1(byte r1) { this.r1 = r1; } public byte getR2() { return r2; } public void setR2(byte r2) { this.r2 = r2; } public byte getR3() { return r3; } public void setR3(byte r3) { this.r3 = r3; } public String getSongName() { return songName; } public void setSongName(String songName) { if (songName == null) { throw new NullPointerException("歌名不能是null!"); } valid = true; this.songName = songName; } public String getAlbum() { return album; } public void setAlbum(String specialName) { this.album = specialName; } public String getYear() { return year; } public void setYear(String year) { this.year = year; }} 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【anselcat】截止到2008-07-09 13:34:33的历史汇总数据(不包括此帖):发帖的总数量:7 发帖的总分数:130 每贴平均分数:18 回帖的总数量:1 得分贴总数量:0 回帖的得分率:0% 结贴的总数量:2 结贴的总分数:40 无满意结贴数:0 无满意结贴分:0 未结的帖子数:5 未结的总分数:90 结贴的百分比:28.57 % 结分的百分比:30.77 % 无满意结贴率:0.00 % 无满意结分率:0.00 % 楼主该结一些帖子了 授尔于渔[url=http://blog.csdn.net/sunshine1314/archive/2008/06/05/2514322.aspx] 采用ID3import java.io.*;public class ReadID3 { public static void main(String[] arguments) { try { File song = new File(arguments[0]); FileInputStream file = new FileInputStream(song); int size = (int)song.length(); file.skip(size - 128); byte[] last128 = new byte[128]; file.read(last128); String id3 = new String(last128); String tag = id3.substring(0, 3); if (tag.equals("TAG")) { System.out.println("Title: " + id3.substring(3, 32)); System.out.println("Artist: " + id3.substring(33, 62)); System.out.println("Album: " + id3.substring(63, 91)); System.out.println("Year: " + id3.substring(93, 97)); } else System.out.println(arguments[0] + " does not contain" + " ID3 info."); file.close(); } catch (Exception e) { System.out.println("Error -- " + e.toString()); } }}仅供参考~ 有没有中间注释的,运行的结果都一样,求有无中间注释的区别在哪里? nonblocking socketchannel是否准备好? 编译出现警告未经检查的类型使用 问个GUI的问题 在ECLIPSE中的Lomboz发布Web模块时出现的错误! 帮忙分析一下一小段代码 jbuilder->exe文件的问题 在jbuilder下编写的程序想在linux下运行,该怎么设置?急,急,急!!!!!!! 一个非常非常简单的问题 如何实现一个文件目录的树形显示? 购物车项目(servlet+javabeans+jsp) JXL操作EXCEL公式的问题
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class ReadMp3 {
private SongInfo info = null;
private RandomAccessFile ran = null;
private File file = null; public ReadMp3() throws FileNotFoundException {
file = new File("./rec/感觉不到你.mp3");
ran = new RandomAccessFile(file, "r");
System.out.println("文件裝載完畢"); } public static void main(String[] args) throws IOException {
ReadMp3 read = new ReadMp3();
byte[] buffer = new byte[128]; read.ran.seek(read.ran.length() - 128); read.ran.read(buffer);
SongInfo info = new SongInfo(buffer);
System.out.println("name:" + info.getSongName() + " year:"
+ info.getYear() + " 歌手:" + info.getArtist() + " 專輯名:"
+ info.getAlbum() + " 備注:" + info.getComment()); }
}
package mp3;/**
* 一个歌曲信息的类的结构表示 这个歌曲是使用ID3V1的信息存储结构的
*
* @author hadeslee
*/
public class SongInfo { private final String TAG = "TAG"; // 文件头1-3
private String songName; // 歌曲名4-33
private String artist; // 歌手名34-63
private String album; // 专辑名61-93
private String year; // 年94-97
private String comment; // 备注98-125
private byte r1, r2, r3; // 三个保留位126,127,128
private boolean valid; // 是否合法
public transient String fileName; // 此歌曲对应的文件名,没有封装 public SongInfo(byte[] data) {
if (data.length != 128) {
throw new RuntimeException("数据长度不合法:" + data.length);
}
String tag = new String(data, 0, 3);
// 只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("TAG")) {
valid = true;
songName = new String(data, 3, 30).trim();
artist = new String(data, 33, 30).trim();
album = new String(data, 63, 30).trim();
year = new String(data, 93, 4).trim();
comment = new String(data, 97, 28).trim();
r1 = data[125];
r2 = data[126];
r3 = data[127];
} else {
valid = false;
}
} public SongInfo() {
} /**
* 返回是否合法
*
* @return 是否
*/
public boolean isValid() {
return valid;
} /**
* 得到此对象的128个字节的表示形式
*
* @return
*/
public byte[] getBytes() {
byte[] data = new byte[128];
System.arraycopy(TAG.getBytes(), 0, data, 0, 3);
byte[] temp = songName.getBytes();
System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length);
temp = artist.getBytes();
System
.arraycopy(temp, 0, data, 33, temp.length > 30 ? 30
: temp.length);
temp = album.getBytes();
System
.arraycopy(temp, 0, data, 63, temp.length > 30 ? 30
: temp.length);
temp = year.getBytes();
System.arraycopy(temp, 0, data, 93, temp.length > 4 ? 4 : temp.length);
temp = comment.getBytes();
System
.arraycopy(temp, 0, data, 97, temp.length > 28 ? 28
: temp.length);
data[125] = r1;
data[126] = r2;
data[127] = r3;
return data;
} public String getArtist() {
return artist;
} public void setArtist(String authorName) {
this.artist = authorName;
} public String getComment() {
return comment;
} public void setComment(String comment) {
this.comment = comment;
} public byte getR1() {
return r1;
} public void setR1(byte r1) {
this.r1 = r1;
} public byte getR2() {
return r2;
} public void setR2(byte r2) {
this.r2 = r2;
} public byte getR3() {
return r3;
} public void setR3(byte r3) {
this.r3 = r3;
} public String getSongName() {
return songName;
} public void setSongName(String songName) {
if (songName == null) {
throw new NullPointerException("歌名不能是null!");
}
valid = true;
this.songName = songName;
} public String getAlbum() {
return album;
} public void setAlbum(String specialName) {
this.album = specialName;
} public String getYear() {
return year;
} public void setYear(String year) {
this.year = year;
}}
楼主【anselcat】截止到2008-07-09 13:34:33的历史汇总数据(不包括此帖):
发帖的总数量:7 发帖的总分数:130 每贴平均分数:18
回帖的总数量:1 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:2 结贴的总分数:40
无满意结贴数:0 无满意结贴分:0
未结的帖子数:5 未结的总分数:90
结贴的百分比:28.57 % 结分的百分比:30.77 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主该结一些帖子了
[url=http://blog.csdn.net/sunshine1314/archive/2008/06/05/2514322.aspx]
public static void main(String[] arguments) {
try {
File song = new File(arguments[0]);
FileInputStream file = new FileInputStream(song);
int size = (int)song.length();
file.skip(size - 128);
byte[] last128 = new byte[128];
file.read(last128);
String id3 = new String(last128);
String tag = id3.substring(0, 3);
if (tag.equals("TAG")) {
System.out.println("Title: " + id3.substring(3, 32));
System.out.println("Artist: " + id3.substring(33, 62));
System.out.println("Album: " + id3.substring(63, 91));
System.out.println("Year: " + id3.substring(93, 97));
} else
System.out.println(arguments[0] + " does not contain"
+ " ID3 info.");
file.close();
} catch (Exception e) {
System.out.println("Error -- " + e.toString());
}
}
}仅供参考~