An iPhone app starts a thread using NSThread that plays short audio tick on regular intervals to give pace.

Code that starts and plays sound:

tickingThread = [[NSThread alloc] initWithTarget:self
                                            selector:@selector(playStepTicks:)
                                              object:nil];

-(void)playStepTicks:(id) arg
{
    NSLog(@"Started ticking.");
    NSThread *cur = [NSThread currentThread];
    NSTimeInterval stime = 0.3;
    while (!cur.isCancelled) {
        CFTimeInterval t = CACurrentMediaTime();
        NSLog(@"Tick: %f", t);
        AudioServicesPlaySystemSound (tickSound);
        t = CACurrentMediaTime() - t;
        [NSThread sleepForTimeInterval:stime-t];
    };
    NSLog(@"Stopped ticking.");
}

Audio loop runs very precisely on iOS6 and is not distracted by UI events when the screen is on.

We enabled proximity sensor on iPhone 4S to save power.

Unfortunately this has an unpleasant effect on the audio loop: ticks are played with large jitter (see video on YouTube).

How can this problem be solved on iOS 6?

有帮助吗?

解决方案

After asking for help on Apple's developer forum, we decided to use Audio Units to generate continuous audio signal and properly set its amplitude (zero for silence and constant for tick) to achieve the tick effect.

The code was written based on iOS Tone Generator.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top