I have recently had the same requirement, and although I know of a way to do this on android and ios/mac I wanted a way that would work whereever e.g. cocos2d-html5 so I did a little bit of googling and found out that you can get the length of the sound file by using this command (on stackoverflow… cannot find the original )
# call me with audiolength.sh directory
# e.g. ./audiolength.sh .
# or ./audiolength.sh my-mp3-collection
for file in $1/*.caf
ffmpeg -i "$file" 2>&1 | egrep "Duration"| cut -d ' ' -f 4 | sed s/,//
The shell script prints out the file names and the durations of all my files - I then tidy the output up into a json file and use it for looking up audio file durations at runtime…
I know this probably sounds cluncky, but it works really well and removes the need for relying on the api.