Callback at certain times during audio playback
Is it possible to play an audio file from the user's ipod library and have a callback occur开发者_开发百科 whenever the player reaches a certain time point ? I need it to be very accurate, so simply using methods like currentPlaybackTime
might not be enough because float equality is inaccurate. I could use the A - B < Epsilon
float check, but is there a better, more accurate way of achieving this?
If you can target iOS 4.0 or hight, try using AVPlayer. Then you will be able to use
- (id)addBoundaryTimeObserverForTimes:(NSArray *)times queue:(dispatch_queue_t)queue usingBlock:(void (^)(void))block
which takes an array of NSValues for CMTimes and will run the contents of the block each time one of the boundary times is hit. (Beware of some behavior like, if you pause the audio file inside of the block, the callback will fire again when you unpause it).
Since CMTime is not a float, I think this will be more accurate than checking for the currentTime repeatedly.
精彩评论