How to check current time and duration in AudioQueue

How to get the total time duration of music in audioQueue. I use

NSTimeInterval AQPlayer::getCurrentTime() { NSTimeInterval timeInterval = 0.0; AudioQueueTimelineRef timeLine; OSStatus status = AudioQueueCreateTimeline(mQueue, &timeLine); if(status == noErr) { AudioTimeStamp timeStamp; AudioQueueGetCurrentTime(mQueue, timeLine, &timeStamp, NULL); timeInterval = timeStamp.mSampleTime; } return timeInterval; } 

AudioQueueGetCurrentTime (mQueue, timeLine, & timeStamp, NULL); To get the current playing time, it gives some great importance - it is valid and how to get the duration of a music file.

+4
source share
3 answers

In the future, I get the correct time in seconds using a small modification of the Chandan code:

 int AQPlayer::GetCurrentTime() { int timeInterval = 0; AudioQueueTimelineRef timeLine; OSStatus status = AudioQueueCreateTimeline(mQueue, &timeLine); if(status == noErr) { AudioTimeStamp timeStamp; AudioQueueGetCurrentTime(mQueue, timeLine, &timeStamp, NULL); timeInterval = timeStamp.mSampleTime / mDataFormat.mSampleRate; // modified } return timeInterval; } 
+7
source

AudioQueueGetCurrentTime (mQueue, timeLine, & timeStamp, NULL); to get the current playing time, it gives some great importance, is it really

Probably, but thatโ€™s not what you think. It is not in seconds; the documents do not say what it is, but googling around, apparently, in frames. (For example, this technot contains a fragment that treats it as frames.) Try dividing by the sampling rate and dividing by the (source) frame rate and see which one you get reasonable numbers.

how to get the duration of a music file.

Not. A sound queue is simple: a queue of sound bites to play or record. The only queue length is the number of samples that you could queue; the queue does not know the length of everything that could be absorbed into it, if these sources even have a finite length.

The audio queue calls the function you create to receive audio samples from you. Wherever your function gets samples from (e.g. AudioFile), you need to get the length. If you yourself generate samples (as in a tone or noise generator), then the length, if any, is up to you.

+2
source
 NSTimeInterval AQPlayer::getTotalDuration() { UInt64 nPackets; UInt32 propsize = sizeof(nPackets); XThrowIfError (AudioFileGetProperty(mAudioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets), "kAudioFilePropertyAudioDataPacketCount"); Float64 fileDuration = (nPackets * mDataFormat.mFramesPerPacket) / mDataFormat.mSampleRate; return fileDuration; } 
+1
source

All Articles