我正在编写一个应用程序,它不仅需要检测用户摇动一次,而且需要持续检测。这个想法是声音会在一次摇晃时播放一次,如果设备持续摇晃,声音会循环播放。
我已经使用 Shake API 和 Accelerometer API 对其进行了测试,但都没有完全符合我的要求。这是我到目前为止所得到的:
- (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");
}
}