Java Sound File durationMillis(String audioPath)

Here you can find the source of durationMillis(String audioPath)

Description

Returns the duration in milliseconds of the audio file whose path is specified.

License

Open Source License

Parameter

Parameter Description
audioPath - a path to an audio file

Return

the duration in milliseconds of the audio file

Declaration

static int durationMillis(String audioPath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class Main {
    /**//from w  w  w .  jav  a2  s .c  o  m
     * Returns the duration in milliseconds of the audio file whose path is specified.
     * 
     * @param audioPath
     *            - a path to an audio file
     * @return the duration in milliseconds of the audio file
     */
    static int durationMillis(String audioPath) {
        int durationMillis = -1;
        File file = new File(audioPath);
        try (AudioInputStream audioInputStream = AudioSystem
                .getAudioInputStream(file)) {
            AudioFormat format = audioInputStream.getFormat();
            long frames = audioInputStream.getFrameLength();
            durationMillis = (int) Math.ceil((frames + 0.0)
                    / format.getFrameRate() * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return durationMillis;
    }
}

Related

  1. sliceWav(InputStream is, long starttime, long duration)
  2. getAudioDuration(final URI wavFile)
  3. getAudioDuration(final URI wavFile)