java获取音频时长

时间:2023-01-03 19:45:39

下载 jave-1.0.2.jar

http://pan.baidu.com/s/1nueaBjF


File source =new File('d:\\test.mp3');
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration();
duration = ls/1000;
System.out.println("此视频时长为:"+ls/60000+"分"+(ls/1000-ls/60000*60)+"秒!");


若用的是springmvc框架上传文件

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
 Iterator<String> iter = multiRequest.getFileNames();

while(iter.hasNext()){

          MultipartFile file = multiRequest.getFile(iter.next());
          String fileOldName = file.getOriginalFilename();

          long duration = 0;//音频长度,秒

          CommonsMultipartFile cf= (CommonsMultipartFile)file;
          DiskFileItem fi = (DiskFileItem)cf.getFileItem();
          File source = fi.getStoreLocation();
          Encoder encoder = new Encoder();
          MultimediaInfo m = encoder.getInfo(source);
          long ls = m.getDuration();
          duration = ls/1000;

}