用Java查找MP3文件的持续时间 [英] Finding Duration of an MP3 file in Java

查看:91
本文介绍了用Java查找MP3文件的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我尝试使用ID3标签来获取持续时间,我也尝试使用JMF媒体播放器.getDuration()。

OK so I have tried using ID3 tags to get the duration and I also tried using JMF media player.getDuration().

player.getDuration().getSeconds()

该文件为VBR。 JMF中是否有任何轻量级库或其他东西可以用来解决这个问题。

The file is VBR. Are there any light weight libraries or something inside JMF that could be used to solve this problem.

谢谢。

推荐答案

我使用JAudioTagger来实现这一目标。下面的代码将为您提供MP3曲目的持续时间。

I use JAudioTagger to achieve this. The below code will get you the duration of an MP3 track.

int duration = 0;

try {
  AudioFile audioFile = AudioFileIO.read(new File("file.mp3"));
  duration = audioFile.getAudioHeader().getTrackLength();

} catch (Exception e) {
  e.printStackTrace();

}

您也可以将audioFile.getAudioHeader()转换为MP3AudioHeader和使用方法getPreciseTrackLength()来获得更精确的持续时间。但是,这(我相信)仅适用于MP3文件而不适用于其他格式(例如WAV文件)。

You can alternatively cast audioFile.getAudioHeader() to MP3AudioHeader and use the method getPreciseTrackLength() to get a more precise duration. However, this (i believe) only applies to MP3 files and no other formats (such as WAV files).

这篇关于用Java查找MP3文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆