JAVA编程获取音频时长

具体代码如下:

File source =new File('C:/Users/Qiang/Desktop/Alone.mp3'); 
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration();
duration = ls/1000;
System.out.println("time is:"+ls/60000+"min"+(ls/1000-ls/60000*60)+"sec");
使用SpringMVC框架上载文件,代码如下:

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request; 
 Iterator 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;}
所用的jar包:jave-1.0.2.jar,自行百度下载导入即可。

你可能感兴趣的:(Java后端)