views:

457

answers:

3

Hi

I have a linear PCM file (.wav) in my app and I would like to get it's duration programatically. I could find the following using AudioStreamBasicDescription but could not make out how to get the duration in seconds.

mFormatID
mSampleRate
mChannelsPerFrame
mBytesPerPacket
mFramesPerPacket
mBytesPerFrame
mBitsPerChannel

Can someone please tell me how I can get the duration.

Thanks.

+1  A: 

You can create an AVAudioPlayer instance with your file. AVAudioPlayer has duration property.

Morion
Nothing using audio streams?
lostInTransit
Hmm... You can use AudioFileGetProperty method and check value of kAudioFilePropertyEstimatedDuration property.
Morion
Agreed. For PCM, getting the kAudioFilePropertyEstimatedDuration should give you an accurate duration, since it's constant bit rate, and can therefore be calculated exactly from the ASBD and the number of samples.
invalidname
A: 

Using AudioFile kAudioFilePropertyEstimatedDuration property is best, however if you want the math for PCM: duration = totalNumPackets * ASBD.mFramesPerPacket / ASBD.mSampleRate

eddienull
A: 

Hi,

I am also using kAudioFilePropertyEstimatedDuration property to get duration . but in what format it will give duration. ?? and in which parameter it will give output. ?

Thanks in advance,

Mitul

mitul shah
You're asking question in the answer here. Ask as a separate question.
Sagar