Eric So
Eric So

Reputation: 465

iOS Audio unit get current time stamp

I'm using audio unit (iOS) to play music from file. How do I get the current time stamp of the music I m playing?

I found that there is a variable call "inTimeStamp" of type AudioTimeStamp in the playbackCallback function. Is it the right place i look for the current time stamp?

Upvotes: 0

Views: 2147

Answers (2)

lixing123
lixing123

Reputation: 78

loretoparisi's answer is not suited for all scenes. If you're using kAudioUnitSubType_AudioFilePlayer, then try his answer; but if you're using other audio unit types such as RemoteIO unit, you need to set a global variable that stores the frame count that the audio unit has rendered, and update the value in every render cycle.

player.progress += inNumberFrames/player.canonicalFormat.mSampleRate;

Upvotes: 1

loretoparisi
loretoparisi

Reputation: 16271

Here you are:

AudioTimeStamp ts;
UInt32 size = sizeof(ts);

AudioUnitGetProperty(THIS->audioPlayerUnit,
                     kAudioUnitProperty_CurrentPlayTime, 
                     kAudioUnitScope_Global, 
                     0, &ts, &size);

NSLog(@"TS%f", ts.mSampleTime);

A better way to get the seconds is to add

THIS->currentTime = ts.mSampleTime / THIS.streamFormatDescription.mSampleRate;

Upvotes: 4

Related Questions