2011-04-22 60 views
0

我创建了一个音乐应用程序,当对象与网格边缘碰撞时播放9种声音之一。这在模拟器上绝对完美无瑕,但在第四代设备上并不完全同步,并且在iPhone 3g上完全不同步。在不同iOS设备上同步声音的问题

由于不同步,我的意思是混响每隔0.2秒发生一次以匹配网格移动的速度,并且由于混响并非同时在设备上,所以声音听起来不正确。从查看iPhone 3g中可以看出,网格绝对不会每0.2秒重绘一次 - 速度要慢得多。

这是基本的代码:

- (void)startTime { 
    [musicTimer setFireDate:[NSDate distantFuture]]; 
    musicTimer = [NSTimer scheduledTimerWithTimeInterval: 0.01 
               target: self 
               selector: @selector(checkTime) 
               userInfo: nil 
               repeats: YES]; 
} 

- (void)checkTime { 
    float timeSince = [[NSDate date] timeIntervalSinceDate:lastPlayed]; 
    if(timeSince >= 0.2){ 
     [self repositionBlocks]; 
    } 
} 

- (void)repositionBlocks { 
    //Check which sounds need to play and call the play function on each of them 
    //The following line would play the sound if a collision occurred 

    Sound *sound = [[Sound alloc] init]; 
[sound play:@"01.wav"]; 
[sound release]; 

    [self redrawGrid]; //Redraws the grid with the new positions 
    [lastPlayed release]; 
    lastPlayed = [[NSDate date] retain]; 
} 

这里是我的Sound类

- (void)play:(NSString *)soundFile { 
    NSString *path; 
    NSString *fileName = [NSString stringWithFormat:@"/%@", soundFile]; 
    path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], fileName]; 
    SystemSoundID soundID; 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
    [filePath release]; 
} 

内播放功能,如果有人可以帮助所有有这个问题,我将非常感谢

谢谢

编辑:我已经做了一些测试并将其缩小到NSTimer的问题,在模拟器上它每隔0.2秒触发一次,但在设备上每触发0.4-0.6秒。我在网上搜索过,有很多关于NSTimers不准确的信息,不应该用于这个,但我找不到任何其他的选择。

+0

只是好奇,但为什么设置NSTimer为0.01,然后在checkTime检查0.2? NSTimer不是比你需要的更频繁地发射? – onnoweb 2011-04-22 19:30:13

+0

本来我试过它开火0.2秒,但我有同样的问题,我读了一些地方,我应该更频繁地开火,并检查是否0.2秒过去了。无论哪种方式,问题似乎都是一样的 – treeba 2011-04-22 19:59:00

回答

0

使用CADisplayLink设置帧率;它比使用NSTimer准确得多。

您也可以尝试使用AVAudioPlayer缓存您的声音 - 在加载应用程序或屏幕时,在每个缓存声音上调用prepareToPlay,然后当您稍后调用播放时,它应该立即播放而不会延迟。 AVAudioPlayer处理缓存声音或从存储等流媒体。