iOS 5 检测重复/连续摇动

发布于 2024-12-27 11:25:04 字数 727 浏览 5 评论 0原文

我正在编写一个应用程序,它不仅需要检测用户的摇晃一次,而且需要连续检测。这个想法是,声音会在一次摇动时播放一次,如果设备连续摇动,声音会循环播放。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文