How to sync animations with sound on iPad
I have a page on an iPad that is setup so there are a bunch of characters and background music. Tapping on a character will start an animation and the character will play along with the music. The problem is, the music and animations are getting out of sync and there's probably a better way.
Here is how I'm doing it.
• page loads • I add the still versions of the characters as UIImageViews • I create AVAudioPlayer objects for each of the character's inst开发者_高级运维ruments, start them playing and set volume to 0. • I add the animated versions of the characters as UIImageViews with a png sequence, position above the still versions and hide them. (there are buttons that throw visibility and volume switches)When I demo this, the audio starts playing before the page loads and some characters get really out of sync. What should I be doing differently? How can I sync these up? Is this the totally wrong approach? I'm kind of new at this.
I'm not sure if this will fix your specific problem (since I don't fully understand what you're trying to do), but generally you should create your set of AVAudioPlayer
objects and then call [myPlayer prepareToPlay]
on each of them. This call loads the data into buffers ready to play, but does not actually start them. Then, when you're ready to start them all playing (in viewDidLoad
or wherever), call [player play]
on them at the same time.
Note that this will probably only work better than what you have currently, but it still won't work perfectly. Keeping animation and music in perfect sync is extremely difficult, and generally can only be accomplished by use of a timer that occasionally resets its periodicity in response to information from the audio playback engine about where exactly audio playback currently is.
I know the quest is old, but using a CAKeyframeAnimation
might help.
精彩评论