Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top