0

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");
    }
}
Brian
  • 723
  • 1
  • 8
  • 26
  • 1
    Try out apples' shake to shuffle feature and you'll find even that does not reproduce reliably. Sometimes a violent amount of shaking is needed, other times just a gentle vibration is sufficient. If you have multiple iphones from different generations the difference is more pronounced. I would imagine you will encounter the same problems with your own implementation. – Abdul Hfuda Jan 16 '12 at 22:17
  • Note that `accelerometer:didAccelerate:` is deprecated as of iOS 5. – Pascal Aug 19 '12 at 14:47

1 Answers1

0

It looks like Apple has a shake gesture. See if this helps:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html

Daren
  • 787
  • 2
  • 7
  • 18