Java获取网络MP3音乐文件时长

时间:2022-11-19 05:03:57

直接上代码:

public void method3() throws Exception {
URL urlfile = new URL("http://www.greenworm.com/ising/song02/135_158.mp3");
//File file = new File("C:\\music\\test2.mp3");
//URL urlfile = file.toURI().toURL();
URLConnection con = urlfile.openConnection();
int b = con.getContentLength();// 得到音乐文件的总长度
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
Bitstream bt = new Bitstream(bis);
Header h = bt.readFrame();
int time = (int) h.total_ms(b);
System.out.println(time / 1000);
}

urlfile为网络mp3的真实链接,换成你们自己的,如果想获取本地的就需要转换一下

File file = new File("C:\\music\\test2.mp3");
URL urlfile = file.toURI().toURL();

需要借助JLayer类库实现,源码下载地址:http://download.csdn.net/detail/jys1109/5871797