iOS 5 Detect Repeating/Continuous Shake

750 Views Asked by At

I am writing an app that will need to detect a users shake not only once but continuously. The idea is that a sound would play once on a single shake and the sound would loop if the device is continuously shaken.

I have tested it with the both the Shake API and Accelerometer API but neither do exactly what I want. Here is what I've got so far:

- (void)playAudioFile
{
    soundFile = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"wav"]];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    [audioPlayer setDelegate:self];
    [audioPlayer play];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    if (acceleration.x > 0.5 || acceleration.y > 0.5 || acceleration.z > 0.5) {
        [self playAudioFile];
        NSLog(@"Trigger @ 0.5x");
    }
}
1

There are 1 best solutions below

0
On