AVAudioPlayerNode position

Dear all,


I'm currently studying the AVAudioEngine API in order to gradually transfer my apps from the old and soon-to-be-deprecated AUGraph API... and I'm facing huge issues at the very beginning of my exploration of AVAudioEngine.

The general context of my exploration so far is a music playing with players, effects, taps and mixer.

1. Accessing curent position of an AVAudioPlayerNode

I simply want to access the current AVAudioPlayerNode's position within the scheduled buffer or file. After some research, I found the following way to access to current frame position of an AVAudioPlayerNode :

AVAudioTime *nodeTime=self.playerNode.lastRenderTime;
AVAudioTime *playerTime=[self.playerNode playerTimeForNodeTime:nodeTime];
AVAudioFramePosition framePosition = playerTime.sampleTime;


My problem is that the playerTimeForNodeTime function returns nil if AVAudioPlayerNode is paused. So in the following basic scenario:

S1 schedule a file or buffer in an AVAudioPlayerNode,

S2 play the AVAudioPlayerNode,

S3 pause the AVAudioPlayerNode,

S4 play (resume) the AVAudioPlayerNode,

I'm completely blind about the position of the AVAudioPlayerNode within the scheduled file or buffer between the steps S3 and S4, even though, according to AVAudioPlayerNode's documentation: "The player's sample time does not advance while the node is paused.", unlike the stop function, which flushes all scheduled buffers/files.


Is there a way to get access to the AVAudioPlayerNode's current position while in paused state? The only solution I see so far is to make a new class to store the current position when the pause function is called in order to be able to access it while the AVAudioPlayerNode is paused, which seems not very elegant and makes me think that I'm in the wrong way...


2. Seeking in a file scheduled in a AVAudioPlayerNode


Still in my music player context, I want to to schedule audio files in my AVAudioPlayerNodes and seek within those files while playing. The only solution I found so far is the following:

BOOL wasPlaying = playerNode.isPlaying;
[playerNode stop];
self.playerNode scheduleSegment:audioFile startingFrame:newFramePosition frameCount:(AVAudioFrameCount)(audioFile.length-newFramePosition) atTime:nil completionHandler:nil];
if (wasPlaying) [playerNode play];


Again, this seems weird and not very elegant.... Is there a proper way to seek within a file or buffer currently playing in a AVAudioPlayerNode?



Finally, my guess after my first experiments is that AVAudioPlayerNode is not designed for music playback, given the two basic problems I faced. So my final question is: What could be the proper solution to play music with custom effects/taps/mixers within the AVAudioEngine framework?


Thank you all for your attention and help 🙂

Best


Tom

AVAudioPlayerNode position
 
 
Q