java获取网络音频时长--不是很准
1 <dependency> 2 <groupId>com.badlogicgames.jlayer</groupId> 3 <artifactId>jlayer</artifactId> 4 <version>1.0.2-gdx</version> 5 </dependency>
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.Header;
public static void main(String[] args) { try { long startTime=System.currentTimeMillis(); //获取开始时间 URL urlfile = new URL("http://resource.puxinwangxiao.com/b4ef18fe62948ab2528127c8c1357ddd.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); long endTime1=System.currentTimeMillis(); //获取结束时间 System.out.println("所需时间: "+(endTime1-startTime)+"ms"); }catch (Exception e ){ System.out.println(e.getMessage()); } }
经测试,此方法耗时少,但计算的时长有偏差:
1439s 。计算出来是 1479s
490s。计算出来是491s