2012-01-16 77 views
0

我正在编写一个应用程序,它需要检测用户不仅一次而且连续地震动。这个想法是,一次振动就会播放一次声音,如果设备不断震动,则声音会循环播放。iOS 5检测重复/连续抖动

我已经用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"); 
    } 
} 
+1

试用苹果摇动以随机播放功能,你会发现即使是不可靠地重现。有时需要剧烈的晃动,其他时候只需轻微的震动就足够了。如果你有不同世代的多个iphones,这个差别就更明显了。 我会想象你会遇到与你自己的实现相同的问题。 – 2012-01-16 22:17:44

+0

请注意,从iOS 5开始不推荐使用'accelerometer:didAccelerate:'。 – Pascal 2012-08-19 14:47:16

回答