Im running a simple little core audio audio using ios 6.0.
The problem I have is when requesting the kAudioUnitProperty_CurrentPlayTime of the file player unit. When I run my code on the simulator everything runs fine. If I constantly poll the property I can see it rise from -1, 0 , 2048 etc .
When I run the application on a device though I get very puzzling results. Instead of seeing a steady increase in the current play time I see output similar to -1,0,
2144484098.
So, on a device the current play time leaps ahead to what seems like an arbitrary huge number.
Can anybody explain this simulator vs device behavior or explain how the file players timeline could jump ahead like this?
Float64 getTrackPosition() {
AudioTimeStamp ts;
UInt32 size = sizeof(ts);
AudioUnitGetProperty(fileAU, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &ts, &size);
Float64 sampleFrame = ts.mSampleTime;
return sampleFrame ;
}
double PrepareFileAU (AudioUnit &au, CAStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
//
// calculate the duration
UInt64 nPackets;
UInt32 propsize = sizeof(nPackets);
XThrowIfError (AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets), "kAudioFilePropertyAudioDataPacketCount");
Float64 fileDuration = (nPackets * fileFormat.mFramesPerPacket) / fileFormat.mSampleRate;
ScheduledAudioFileRegion rgn;
memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
rgn.mTimeStamp.mSampleTime = 0;
rgn.mCompletionProc = NULL;
rgn.mCompletionProcUserData = NULL;
rgn.mAudioFile = audioFile;
rgn.mLoopCount = 0;
rgn.mStartFrame = 0;
rgn.mFramesToPlay = UInt32(nPackets * fileFormat.mFramesPerPacket);
XThrowIfError (AudioUnitSetProperty(au, kAudioUnitProperty_ScheduledFileIDs, kAudioUnitScope_Global, 0, &audioFile, sizeof(AudioFileID)), "Schedule file Id's");
XThrowIfError (AudioUnitSetProperty(au, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &rgn, sizeof(rgn)), "kAudioUnitProperty_ScheduledFileRegion");
// prime the fp AU with default values
UInt32 defaultVal = 0;
XThrowIfError (AudioUnitSetProperty(au, kAudioUnitProperty_ScheduledFilePrime, kAudioUnitScope_Global, 0, &defaultVal, sizeof(defaultVal)), "kAudioUnitProperty_ScheduledFilePrime");
AudioTimeStamp startTime;
memset (&startTime, 0, sizeof(startTime));
startTime.mFlags = kAudioTimeStampSampleTimeValid;
startTime.mSampleTime = -1;
XThrowIfError (AudioUnitSetProperty(au, kAudioUnitProperty_ScheduleStartTimeStamp, kAudioUnitScope_Global, 0, &startTime, sizeof(startTime)), "kAudioUnitProperty_ScheduleStartTimeStamp");
return fileDuration;
}