java 怎么获取音频文件时长
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioLength {
public static void main(String[] args) throws LineUnavailableException,
UnsupportedAudioFileException, IOException {
File file = new File("d:/test.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
clip.open(ais);
System.out.println( clip.getMicrosecondLength() / 1000000D + " s" );//获取音频文件时长
}
}
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;}