简体   繁体   中英

Sync sound with NStimer iphone sdk?

I am creating an application,in my app i am changing images for countdown.I want to play a Tick sound when one second completes(i mean when image changes).I have a 25 second long sound with repeated tick sound and within time interval of 1 second. sometimes it works fine but sometimes it's not.Now how do i sync audio with my timer.any help appreciated thanks.

record with 25 ticks?) why don't you use record with one tick? if the loudness is not your aim, you can play it using AudioServicesCreateSystemSoundID then call AudioServicesPlaySystemSound every second.

I suggest using AudioServicesCreateSystemSoundID with a sound file that is just one tick, which is about 1/16 of a second long or less, and using this in your init:

clickSound = [[self createSoundWithName:@"click"] intValue];

Here is the code to set up the sound:

- (NSNumber*) createSoundWithName: (NSString*) name {
SystemSoundID soundID = -1;

NSString *path =[[NSBundle mainBundle] pathForResource:name ofType:@"caf"];
if (path != nil) {
    NSURL *url = [NSURL fileURLWithPath:path];
    if (url != nil) {
        OSStatus err = AudioServicesCreateSystemSoundID ((CFURLRef) url, &soundID);
        if (err) NSLog (@"AudioServicesCreateSystemSoundID error %d", err);
    }
}
return [NSNumber numberWithInt:soundID];
}

And here is where you play the click:

- (void) playClick  {
AudioServicesPlaySystemSound (clickSound);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM