How to perform operations when playing sound in iPhone?
avaudioplayer, iphone
Solution
I believe the best solution is to start an `NSTimer` as you start the `AVAudioPlayer` playing. You could set the timer to fire every half second or so. Then each time your timer fires, look at the `currentTime` property on your audio player.
In order to do something at certain intervals, I'd suggest you kept an instance variable for the playback time from last time your timer callback was called. Then if you had passed the critical point between last callback and this, do your action.
So, in pseudocode, the timer callback:
- Get the `currentTime` of your AVAudioPlayer
- Check to see if `currentTime` is greater than `criticalPoint`
- If yes, check to see if `lastCurrentTime` is less than `criticalPoint`
- If yes to that too, do your action.
- Set `lastCurrentTime` to `currentTime`
Problem
I play a MP3 in my iPhone app using AVAudioPlayer; i need to perform some operations at certain times (say 30th seconds, 1 minute); is there a way to invoke callback functions based on mp3 playing time?