0

我正在尝试使用 java sound api 计算 .wav 文件的持续时间。Java 声音 api 支持带有 PCM S16LE 编解码器的 .wav 文件,但它似乎不支持带有 IMA WAV ADPCM 编解码器的 .wav 文件。是否有任何库可以帮助我使用任何类型的编解码器获取各种 .wav 文件的持续时间,或者有什么方法可以在不使用任何库的情况下计算 .wav 文件的持续时间?

到目前为止,我已经尝试了很多:

private Long getDuration(File file)
{
    AudioFileFormat fileFormat = null;

    long duration = 0;
    try
    {
        fileFormat = AudioSystem.getAudioFileFormat(file);

        if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.MP3)) {
            Map<?, ?> properties = fileFormat.properties();
            Long microseconds = (Long) properties.get(StringConstants.DURATION);

            //converting to milliseconds
            duration = microseconds / 1000;
        }
        else if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.AUDIO_WAVE)) {
            long frames = fileFormat.getFrameLength();
            double durationInSeconds = (frames + 0.0) / fileFormat.getFormat().getFrameRate();

            duration = Math.round(durationInSeconds * 1000);
        }
    }

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

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

    return duration;
}

有什么方法可以使用我在这里使用的相同逻辑来计算 IMA WAV ADPCM AUDIO 的 .wav 文件的持续时间?如果是,我如何获得文件的帧长度和帧速率?

4

0 回答 0