简体   繁体   English

AVAudioEngine跟踪音频进度

[英]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. 现在,我想获取AVAudioPlayerNode / AVAudioPCMBuffer的framePosition(进度,消耗的帧,%),以便更新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. 我最近创建了一个可与AVAudioEngine和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). 要访问它,您必须获取玩家lastRenderTime,然后使用(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. 如果要重置播放器计时器(例如,当您搜索文件或切换到新歌曲时),则必须使用播放器的playAtTime方法进行设置。 The playAtTime does not perform any file seeking, it just modifies the players internal timer and then starts playback. playAtTime不执行任何文件查找,它只是修改播放器的内部计时器,然后开始播放。

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 如果您想参考我的用法,请查看我的项目: 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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