(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
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.
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.
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With