animationiOS与声音文件

我有一些需要在非常特定的时间出现在屏幕上的animation,这些animation存储在SQLite数据库中。 我打算做的是使用nstimer来保持时间,并在达到特定时间时popupanimation。

我想使用NSTimer计数声音文件和animation的持续时间,然后当达到某些点popup一个屏幕上的animation。 问题是设置的时间是这样的55.715000秒,所以非常准确,这些都需要与animation播放的音轨同步。

首先,这甚至是可能的,其次,我怎样才能比较这样的具体时间,我似乎面临的问题是代码运行不够快,时间跳跃超过.001秒。

我没有openGLES或Cocos2d的知识,学习这些在时间尺度上是不可行的。

无论您的时间精度如何,您都不会让设备显示频率超过60Hz(每帧16.7毫秒)。 正如我所看到的,你至less有两个select:

1)使用CADisplayLinkcallback来检查audio的播放进度,并在每个animation及时触发。

显示链接定时器的创build方式与常规NSTimers相似:

 -(void)viewDidLoad { // ... self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(scheduleAnimations:)]; [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } - (void)scheduleAnimations:(CADisplayLink *)displayLink { // get playback time from player object // iterate queue of timings // if the timestamp of a timing is equal to or less than the playback time // create its animation for immediate execution // remove it from the queue } 

2)创buildanimation集合,将其各自的beginTime属性设置为适当的触发时间(或者,如果使用隐式或基于块的animation,则使用delay参数)。

 [CATransaction begin]; // iterate collection of timings CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"key"]; animation.startTime = /* time to trigger animation */ animation.removedOnCompletion = NO; [layer addAnimation:animation forKey:nil]; [CATransaction commit]; 

如果您的视觉效果需要与audio完全同步(例如,需要在节拍上出现animation的音乐应用程序),则需要使用以下方法。 它适用于非常旧的iPhone硬件,在运行时基本没有任何问题。

  1. 预渲染您的视觉效果的每个“框架”,以便每一个被存储为全屏图像。 这可以在桌面上完成,也可以在手机上运行渲染逻辑,然后以像素的forms将输出捕获到文件中。

  2. 将每个帧保存为像素数组后,通过iOS上的标准AVAudioPlayer API播放audio。 此API负责处理audio回放,并报告您将用于确定要显示哪个video帧的时间。

  3. 一旦audio播放,获取“时间”,并按照您的video帧率划分,以确定从N个图像arrays中显示哪个图像。 获取图像数据并将其包装在CGImageRef / UIImage中,这将以最佳方式将图像数据闪烁到屏幕上。

如果你想看到这种方法的工作源代码,看看AVSync 。 此示例代码显示了我在app store中使用的名为iPractice的应用程序的实现。 它速度非常快,即使在旧的iPhone 3G上也能以30FPS的速度运行。