public static long getMicrosecond(String srcPath) {
Clip clip = null;
AudioInputStream inputStream = null;
try {
inputStream = AudioSystem.getAudioInputStream(new File(srcPath));
clip = AudioSystem.getClip();
clip.open(inputStream);
return clip.getMicrosecondLength();// 获取微秒级长度
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (clip != null) {
clip.close();
}
}
return -1;
}
public static long getMilsecond(String path) {
Encoder encoder = new Encoder();
try {
MultimediaInfo m = encoder.getInfo(new File(path));
return m.getDuration();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static long getSecond(String path) {
Encoder encoder = new Encoder();
try {
MultimediaInfo m = encoder.getInfo(new File(path));
return m.getDuration()/1000;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
建议使用getMicrosecond,不仅读取速度快,而且精准。
后面两个方法由于太慢,我决定不再使用。