views:

75

answers:

4

Dear all, I have a song say about 5.1 mb ,i want to calculate total duration of song before being played on media player the file format is wave file.

+1  A: 

It isn't really possible based on size. For example, an average MP3 is roughly 1 meg per minute, but if it has been encoded at the full 320 bit rate it can be a lot bigger than that. Your best bet would be to tell your script the size AND duration of the music file.

Sohnee
My problem is that i have only size available of a particular song.i want total duration may be it can be bit roughly .please suggest
angelina
You could (roughly) divide the duration by 10 for a wav, and use the file size as the duration for mp3. i.e. wavTime = fileSize / 10;
Sohnee
A: 

maybe this will help you http://www.schillmania.com/content/projects/soundmanager2/

vlad
+1  A: 

you can try this page : http://www.sounddevices.com/calculator/

for an uncompressed wav - 16 bit, 44.1kHz, stereo, you're looking at 172.2 KB/second, so around 10.332 MB/second. so your file is approx 0.5 minutes

oedo
+2  A: 

You need to decode the song in order to know the length of it:

    URL url = new URL("foo.wav");
    Clip clip = AudioSystem.getClip();
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    clip.open(ais);
    System.out.println(clip.getMicrosecondLength());
Samuel Yung
Hello Samuel, I used this code and got following exception.javax.sound.sampled.LineUnavailableException: line with format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, not supported. Please resolve
angelina
Where is the exception thrown?
Samuel Yung