I'm writing a small app that when you shake the phone it plays a sound, and how hard you shake it determines how long the sound will play for. I've set up 3 Konstants (sorry had to small programming joke) of the accelerometer thresholds kAccelerationThreshold, kAccelerationThreshold2, and kAccelerationThreshold3. Then wrote the methods for them.
Code:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if(acceleration.x > kAccelerationThreshold || acceleration.y > kAccelerationThreshold || acceleration.z > kAccelerationThreshold){
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}if(acceleration.x > kAccelerationThreshold2 || acceleration.y > kAccelerationThreshold2 || acceleration.z > kAccelerationThreshold2){
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.numberOfLoops = 3;
theAudio.delegate = self;
[theAudio play];
}if(acceleration.x > kAccelerationThreshold3 || acceleration.y> kAccelerationThreshold3 || acceleration.z > kAccelerationThreshold3){
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.numberOfLoops = 5;
theAudio.delegate = self;
[theAudio play];
}
}
I havn't paid for the developer license yet, I want to have a full fledged app before I do that, but I've built it and there are no errors everything builds and compiles fine, I just want to know if I'm on the right track as I've just pieced all this info I've found together. If there is an easier way to do this I'm open to ideas! but I just really want to make sure this works as I have no way of testing it.
thanks in advance