转自:http://www.voidcn.com/article/p-euvvhkve-bdu.html
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>javazoom</groupId> <artifactId>jlayer</artifactId> <version>1.0.1</version> </dependency>
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); }
需要借助JLayer类库实现,源码下载地址:http://download.csdn.net/detail/jys1109/5871797