Reputation: 147
I'm still trying to find a way that you can call an action when an AVAudioPlayer
music file hits a specific second.
I know I could use the addPeriodicTimeObserverForInterval
method from the AVPlayer
class, however I do not want to use AVPlayer
since it does not use methods such as playing()
, stop()
, numberOfLoops
etc since I'm currently using them
Is there an alternative method that AVAudioPlayer
uses? If so, how would I do it when the AVAudioPlayer
is into 20 seconds.
Upvotes: 2
Views: 3334
Reputation: 131408
I think your choices are to switch to using AVPlayer, or to "fake it" using an NSTimer
, as suggested by Ashish, above, or some other method.
NSTimers aren't accurate beyond ~1/50th of a second (they run on the main thread and only fire when your app visits the run loop.)
You could try registering for KVO notification of the currentTime
property of your AVAudioPlayer
, and then detecting times that are within a specific second.
KVO notifications will probably be quite CPU-intensive, and may even cause the audio playing to stutter, since the KVO notice will fire every time the audio player changes the currentTime
property, even if it's every 1/1000 of a second.
If you only need resolution within a second, and it doesn't have to be exact, NSTimer is the way to go.
Upvotes: 3