开发者

Problems with syncing sounds on different iOS devices

I have created a music app that plays one of 9 sounds when an object collides with the edge of a grid. This works absolutely flawlessly on the simulator but does not quite sync up on 4th gen devices and is completely out of sync on the iPhone 3g.

By out of sync I mean that the reverb happens every 0.2 seconds to match the speed of the grid movement and because the reverb isn't at the same time on the device the sounds don't sound right. Also from looking at the iPhone 3g you can tell that the grid definitely isn't redrawing every 0.2 seconds - it is much slower.

Here is the basic code:

- (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];
}

Here is the play function inside my Sound class

- (void)play:(NSString *)soundFile {
    NSString *path;
    NSString *f开发者_如何学编程ileName = [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];
}

If anyone could help at all with this issue I would be extremely grateful

Thanks

EDIT: I have done some tests and narrowed it down to a problem with the NSTimer, on the simulator it triggers every 0.2 seconds but on the device it triggers every 0.4 - 0.6 seconds. I have searched on the internet and there is lots of information about how NSTimers are not accurate and shouldn't be used for this but I can't find any kind of alternative.


Use CADisplayLink to set your framerate; it is far more accurate than using an NSTimer.

You might also try caching your sounds by using AVAudioPlayer - during loading of the app or screen, call prepareToPlay on each cached sound, then when you call play later it should playback instantly with no delay. The AVAudioPlayer handles caching the sounds or streaming them from storage, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜