Here you can find the source of durationMillis(String audioPath)
Parameter | Description |
---|---|
audioPath | - a path to an audio file |
static int durationMillis(String audioPath)
//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; } }