5

How to get total time duration of music in audioQueue. I am using

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); for getting current playing time, it gives some large value is it valid and how to get duration of music file.

Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63

3 Answers3

8

For future reference, I am getting a correct time in seconds using a slight modification of Chandan's 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;
}
ThomasRS
  • 8,215
  • 5
  • 33
  • 48
  • This method was added to the AQPlayer (v2.4) found in the Apple SpeakHere example. – ThomasRS Jan 25 '11 at 00:25
  • hey thomas here is a related question.. can you take a look?http://stackoverflow.com/questions/12668803/difference-between-audioqueue-time-and-audioqueue-device-time – abbood Oct 01 '12 at 07:03
2

AudioQueueGetCurrentTime(mQueue, timeLine, &timeStamp, NULL); for getting current playing time, it gives some large value is it valid

Probably, but it's not what you think. It's not in seconds; the docs don't really say what it is in, but Googling around, it appears to be in frames, for whatever reason. (For one example, this technote includes a snippet that treats it as frames.) Try dividing by the sample rate and dividing by the (source's) frame rate, and see which one gets you sane numbers.

how to get duration of music file.

There isn't one. An audio queue is just that: a queue of audio samples to be played or recorded. The only length the queue has is the number of samples you can have queued in it; the queue does not know the length of anything that might be feeding into it, if those sources even have a finite length.

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

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1
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;
}
Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63
  • This will not always give exact results. Not all codecs will have a constant number of frames per packet, and some formats, such as AAC, have what Apple calls "priming" and "remainder" frames. You need to get the packet table from the file to truly calculate an accurate duration. – sbooth Aug 05 '10 at 04:48
  • @sbooth [Here's](http://stackoverflow.com/a/18693593/1971013) my work-around for that. – meaning-matters Sep 09 '13 at 09:03