简体   繁体   English

与NStimer iPhone SDK同步声音?

[英]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. 我正在创建一个应用程序,在我的应用程序中,我正在更改图像以进行倒计时。我想在一秒钟完成后播放滴答声(我的意思是图像改变时)。我有25秒长的声音,并在一定的时间间隔内重复滴答声1秒 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? 记录有25个滴答声吗?)为什么不使用记录有1个滴答声呢? if the loudness is not your aim, you can play it using AudioServicesCreateSystemSoundID then call AudioServicesPlaySystemSound every second. 如果响度不是您的目标,则可以使用AudioServicesCreateSystemSoundID播放,然后每秒调用AudioServicesPlaySystemSound

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: 我建议将AudioServicesCreateSystemSoundID与仅一滴答的声音文件配合使用,该声音文件大约为一秒或更少的1/16秒,并在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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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