简体   繁体   中英

AVAudioEngine track progress of audio

I've setup my audio engine and connected a few nodes, so as to make an audio graph that looks like this:

//
//AVAudioPlayerNode --> AVAudioMixerNode --> AVAudioUnitVarispeed ---> AvAudioOutputNode
//       |                                                         |
// AVAudioPCMBuffer                                             AudioTap
//

Everything is working and the audio engine is running. Now I would like to get the framePosition (progress, consumed frames, %) of the AVAudioPlayerNode/AVAudioPCMBuffer so as to update the current position in my UI. Something like:

[timeEffectNode installTapOnBus:0 bufferSize:4096 format:[timeEffectNode outputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
    //calculate the current node progress here. ???
}];

I'm certain there has to be a way of doing this but I can't seem to find any documentation on how to achieve this?

Any ideas would be appreciated.

I recently created a player that works with AVAudioEngine and AVPCMBufferNode. The player keeps an internal time of total time that the player has been playing. To access it you must get the players lastRenderTime and then use the (sampleTime / sampleRate). This should return total time the player has been playing in seconds.

If you want to reset the players timer (like when you seek through a file or switch to new song) you must set use the player's playAtTime method. The playAtTime does not perform any file seeking, it just modifies the players internal timer and then starts playback.

I hope this helps.

If you'd like a reference as to how I used this, check out my project: https://github.com/danielmj/AEAudioPlayer

Just simply take a snapshot of your current node time with

AVAudioTime *now = player.lastRenderTime;

and then ask your player

[player playerTimeForNodeTime:now];

Of course you can do this in one call

[player playerTimeForNodeTime:player.lastRenderTime];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM