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");
}
}
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