문제

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