Nick
Nick

Reputation: 628

How can I determine when a ScheduledAudioFileRegion has finished playing?

On iOS, I have an Audio Unit of type kAudioUnitSubType_AudioFilePlayer wrapped by an AUNode and connected to a multichannel mixer and remote IO (output) in an Audio Graph.

While the graph is playing I want to loop the file and be able to determine when the AudioFilePlayer has reached the end of the file, so that I can perform an operation before it begins the next iteration of its loop.

In reviewing AudioUnitProperties.h, while there is a completion callback - mCompletionProc- it's only called when the disk has read the file and scheduled it for playback, not when it has actually finished playing back the audio.

So then I considered storing the length of the audio (packets/frames) as a property and, in an input callback attached to another input of the mixer, checking to see if we're at the end of the file. But that callback doesn't get called every frame, so it's likely I'd miss the actual end of the file.

How might I approach this?

Upvotes: 3

Views: 454

Answers (2)

DEADBEEF
DEADBEEF

Reputation: 2260

If you want to be aware when the track reaches the end, you could use a RenderNotify (a notification callback to call when an audio unit is asked to render). In the render callback you get the number of frame currently processed and could add them to your frame property. With a simple check if this property is higher than the number of frames you will be able to know when your track is finished.

Upvotes: 0

Jerry Chan
Jerry Chan

Reputation: 117

please set ScheduledAudioFileRegion.mCompletionProc,

//

ScheduledAudioFileRegion playRegion;
playRegion.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
playRegion.mTimeStamp.mSampleTime = 0;
playRegion.mCompletionProc = XXXXXXXXXXXXXXXX(you proc);
playRegion.mCompletionProcUserData = NULL;
playRegion.mAudioFile = mAudioFile;
playRegion.mLoopCount = 0;
playRegion.mStartFrame = mStartSampleFrame;
playRegion.mFramesToPlay = UInt32(-1);

err = AudioUnitSetProperty(mFilePlayerAU, kAudioUnitProperty_ScheduledFileRegion,
                           kAudioUnitScope_Global, 0, &playRegion, sizeof(playRegion));

Upvotes: 1

Related Questions