如何获取音频文件的持续时间?

时间:2015-05-12 06:44:48

标签: java audio javasound

我希望以秒为单位获取音频文件的持续时间(总时间)。音频文件可以是任何格式的I.E. (.mp3,.wav,.wma等)。

 try {
    File file = new File("C:/Users/Administrator/AppData/Local/Temp/" + 
        "01 - Ab Tere Bin Jee Lenge Hum-(MyMp3Singer.com).mp3");
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
    AudioFormat format = audioInputStream.getFormat();
    long frames = audioInputStream.getFrameLength();
    double durationInSeconds = (frames + 0.0) / format.getFrameRate();
    System.out.println("duration in seconds"+durationInSeconds);
    AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
    Map<String, Object> properties = audioFileFormat.properties();
    System.out.println("properties"+properties.size()+"empy::"+properties.isEmpty());
    for(String key: properties.keySet()){
        System.out.println(key  +" :: "+ properties.get(key).toString());
    }
} catch (Exception e) {
    System.out.println("Exception:::::" + e.getMessage());
}

我尝试使用上面的代码执行此操作,但它给出了:

Exception:::::could not get audio input stream from input file

2 个答案:

答案 0 :(得分:1)

您可能错过了MP3编解码器。您可能想尝试Tritonus: Open Source Java Sound

同时检查相关主题:MP3 playback using Java Sound and Sun MP3 plugin

答案 1 :(得分:0)

音频文件的持续时间通常作为AudioFileFormat的属性提供。来自docs

下表列出了实现中应使用的一些常用属性:

音频文件格式属性

+-------------+------------+-----------------------------------------------+
|Property key | Value type | Description                                   | 
|-------------|------------|-----------------------------------------------|
| "duration"  | Long       | playback duration of the file in microseconds |
| "author"    | String     | name of the author of this file               |
| "title"     | String     | title of this file                            |
| "copyright" | String     | copyright message                             |
| "date"      | Date       | date of the recording or release              |
| "comment"   | String     | an arbitrary text                             |
+-------------+------------+-----------------------------------------------+

请注意应该,这意味着它们是可选的。

还请注意,duration是在微秒中给出的,而不是毫秒!

因此,要获取Java中音频文件的持续时间,可以调用:

final String DURATION = "duration";
Long duration = (Long)audioFileFormat.properties().get(DURATION);

// and because duration is optional, use a fallback
// for uncompressed formats like AIFF and WAVE
if (duration == null
    && audioFileFormat.getFormat().getEncoding() == PCM_SIGNED
    // make sure we actually have a frame length
    && audioFileFormat.getFrameLength() != NOT_SPECIFIED
    // make sure we actually have a frame rate
    && audioFileFormat.getFormat().getFrameRate() != NOT_SPECIFIED
    // check if this is WAVE or AIFF, other uncompressed formats may work as well
    && (audioFileFormat.getType() == WAVE || audioFileFormat.getType() == AIFF)) {

    duration = (long) (audioFileFormat.getFrameLength() / audioFileFormat.getFormat().getFrameRate() * 1000L * 1000L);
}

此摘要将通过duration属性或帧长度和帧速率为您提供持续时间(以微秒为单位)。 后者仅适用于未压缩的格式,例如WAVE或AIFF

由于当前的JVM不支持mp3,因此您需要安装mp3编解码器。这以实现Service Provider Interface (SPI)的服务提供商的形式出现。 Tritonus库是mp3的SPI的纯Java实现。有关Maven的用法,请参见例如here。请注意,该库显然早已被废弃。在Mac上使用CoreAudio的最新软件包是CASampledSP。另一个基于FFmpeg并为Mac和Windows编写的库是FFSampledSP(完全公开:我是这两个库的作者)。

获得持续时间的另一种非常规方法是使用JavaFX,因为它开箱即用地支持mp3:

import java.io.File;
import javafx.scene.media.Media;
import javafx.util.Duration;

final Media media = new Media(new File("somefile.mp3").toURI().toString());
final Duration duration = media.duration();
相关问题