从 Java 中的 URL 获取音频持续时间



我需要从java中的音频URL中获取音频的持续时间,但找不到方法。 任何人都可以建议如何做到这一点吗?

我认为应该这样做:

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new URL("http://www.url.com/"+ args[0] +".mp3"));
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));

经过一些研究,我找到了这个问题的答案,发布了答案,以便其他人也可以这样做。

String filePrefix = FileUtils.getTempDirectoryPath() + File.separator;
URL audioURL = new URL(url);
File destination = new File(filePrefix + UUID.randomUUID().toString());  // To store the file at a certain destination for temporary usage
FileUtils.copyURLToFile(audioURL, destination);
IsoFile isoFile = new IsoFile(destination.getAbsolutePath());
double lengthInSeconds = (double)
isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
try {
System.out.println("Length of audio in seconds === " + lengthInSeconds);
destination.delete();
} catch (Exception e) {
e.printStackTrace();
}

注意:FileUtils 来自 apache.commons 存储库,IsoFile 来自 coremedia.iso 存储库

最新更新