我正在尝试以越来越高的频率背靠背播放两个音频文件(点击和咔嗒声)。它按照第一秒的需要工作,然后声音同时开始播放。知道我在这里做错了吗?
static float clickClackDelay = 1.0;
- (void)runClickClackTimer {
clickURL = [[NSBundle mainBundle] URLForResource:@"click" withExtension:@"wav"];
clackURL = [[NSBundle mainBundle] URLForResource:@"clack" withExtension:@"wav"];
avClick = [[AVAudioPlayer alloc] initWithContentsOfURL:clickURL error:nil];
avClack = [[AVAudioPlayer alloc] initWithContentsOfURL:clackURL error:nil];
clickClack = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
target:self
selector:@selector(runClickClackTimer)
userInfo:nil
repeats:NO];
[avClick play];
sleep(clickClackDelay);
[avClack play];
sleep(clickClackDelay);
NSLog(@"Play sound every %.2f", clickClackDelay);
clickClackDelay -= 0.01;
}
答案 0 :(得分:1)
首先在.h文件中定义BOOL
实例:
BOOL play;
现在将此timer
添加到需要play sound in delay
。
NSTimer *clickClack = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
target:self
selector:@selector(runClickClackTimer)
userInfo:nil
repeats:YES];
根据clickClackDelay播放延迟的方法是:
-(void)runClickClackTimer
{
if(play)
{
play = FALSE;
[avClick play];
}
else
{
play = TRUE;
[avClack play];
}
}
答案 1 :(得分:0)
第一个睡眠定时器和NSTimer都将延迟相同的时间,1秒,因此调用
[avClack play];
在runClickClackTimer的第一个实例中
[avClick play];
在runClickClackTimer的第二个实例中。
我会设置两个独立的NSTimers来调用两个独立的函数,可能是runClickTimer和runClackTimer。