Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a sound file's total time in Java?

Tags:

java

file

audio

How do I get a sound file's total time in Java?

--UPDATE

Looks like this code does de work: long audioFileLength = audioFile.length();

    recordedTimeInSec = audioFileLength / (frameSize * frameRate); 

I know how to get the file length, but I'm not finding how to get the sound file's frame rate and frame size... Any idea or link?

-- UPDATE

One more working code (using @mdma's hints):

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);     AudioFormat format = audioInputStream.getFormat();     long audioFileLength = file.length();     int frameSize = format.getFrameSize();     float frameRate = format.getFrameRate();     float durationInSeconds = (audioFileLength / (frameSize * frameRate)); 
like image 830
The Student Avatar asked Jun 09 '10 21:06

The Student


People also ask

What audio files can Java play?

Java API Support for MP3 Format. Currently, both Clip and SourceDataLine can play audio files in AIFC, AIFF, AU, SND, and WAV formats. We can check the supported audio format using AudioSystem: Type[] list = AudioSystem.


1 Answers

Given a File you can write

File file = ...; AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); AudioFormat format = audioInputStream.getFormat(); long frames = audioInputStream.getFrameLength(); double durationInSeconds = (frames+0.0) / format.getFrameRate();   
like image 179
mdma Avatar answered Sep 23 '22 13:09

mdma