Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an audio file's length (in seconds)

(Objective C) Just using simple AudioServicesPlaySystemSoundID and its counterparts, but I can't find in the documentation if there is already a way to find the length of an audio file.

I know there is AudioServicesGetPropertyInfo, but that seems to return a byte-buffer - do audio files embed their length in themselves and I can just extract it with this?

Or is there perhaps a formula based on bit-rate * fileSize to convert to length-of-time?

mIL3S

www.milkdrinkingcow.com

like image 969
Miles Alden Avatar asked Jan 07 '11 20:01

Miles Alden


People also ask

How do I find the length of an audio file?

To determine the file size of an audio file, we have to multiply the bit rate of the audio by its duration in seconds. As a result, we get file size values in terms of kilobits and megabits.

How can I tell the frequency of a WAV file?

If you have a . wav file you can calculate the frequency directly. If the file is a single pure tone simply count n the number of samples between successive 0 crossings and divide 44.1/n to get the frequency in kHz. If the file is a mix of tones then you will need to do a Fourier transform.


1 Answers

According to a quick Google search, there is a formula:

length-of-time (duration in seconds) = fileSize (in bytes) / bit-rate (bits/secs)*8

Is there any particular reason you're using System Sound Services to play a sound?

If you use AVAudioPlayer to handle your sounds you could do something like:

AVAudioPlayer * sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
sound.delegate = self;
sound.volume = 1;
NSLog([NSString stringWithFormat:@"%f", sound.duration]);
like image 100
Poff Avatar answered Oct 21 '22 04:10

Poff