如何读取mp3的信息?

superliyubo 2009-06-27 01:49:16
如何用JAVA读取 mp3文件里包含的 文件名 这类的信息
谢谢~~~~~~~~~~~
...全文
80 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
14test41 2009-06-27
  • 打赏
  • 举报
回复
学习中。。。。
wanglingzhong 2009-06-27
  • 打赏
  • 举报
回复
可以从音乐文件的最后字节中读出!若采用ID3
ID3标签是MP3音乐档案中的歌曲附加讯息,它能够在MP3中附加曲子的演出者、作者以及其它类别资讯,方便众多乐曲的管理

import 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());
}
}
}

因为ID3版本较多,这一个程序对某些歌曲不对!仅供参考吧~~
j1223jesus 2009-06-27
  • 打赏
  • 举报
回复
楼上速度真快。。。
valen_jia 2009-06-27
  • 打赏
  • 举报
回复

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;

public class ParseInput {
public static void main(String[] args){
File file = new File("E:/KwDownload/song/张韶涵-隐形的翅膀.mp3");
System.out.println("文件名:"+file.getName());
System.out.println("文件大小:"+getFileSize(file));
System.out.println("文件绝对路径:"+file.getPath());
System.out.println("是否隐藏:"+file.isHidden());
System.out.println("最后一次修改时间:"+new SimpleDateFormat("yyyy-MM-dd").format(file.lastModified()));
}

public static String getFileSize(File file) {
String result = "";
try {
Double d = 0d;
InputStream in = new FileInputStream(file);
int oneSize = in.available();
d += oneSize;
in.close();
Double dd = Double.parseDouble(d + "") / (1024);
String size = dd + "";
String[] str = size.split("\\.");
// 不到1K
if (str[0].equals("0")) {
result = dd + "字节";
}
// 1k~1M
else if (str[0].length() >= 1 && str[0].length() < 4) {
result = Double.parseDouble(d + "") / (1024) + "00";
result = result.split("\\.")[0] + "."
+ result.split("\\.")[1].substring(0, 2) + "Kb";
}
// 1M~1G
else if (str[0].length() >= 4 && str[0].length() < 7) {
result = Double.parseDouble(d + "") / (1024 * 1024) + "00";
result = result.split("\\.")[0] + "."
+ result.split("\\.")[1].substring(0, 2) + "Mb";
}
// 1G以上
else {
result = Double.parseDouble(d + "") / (1024 * 1024 * 1024)
+ "00";
result = result.split("\\.")[0] + "."
+ result.split("\\.")[1].substring(0, 2) + "G";
}
// System.out.println("大小为:" + result);

} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧