【问题标题】:Need help using Accelerometer in iOS在 iOS 中使用 Accelerometer 需要帮助
【发布时间】:2017-10-21 17:54:43
【问题描述】:

我正在开发一款应用程序,该应用程序会在用户持有其 iOS 设备的每个位置(站立、正面/背面或侧面)时播放哔声。目前,当用户将设备放在一边时,我可以播放声音,但是,问题是因为我有与滑块链接的加速度计值,所以哔哔声是连续的(即它播放声音为只要用户将设备侧放),而不仅仅是一次。

我希望用户只需将设备稳定地放在一边,然后发出一声哔哔声,然后让用户将设备依次保持在其他位置,然后等待另一个哔哔声。我希望用户一步一步地走,一次一个地把设备放在每个位置,只有在听到哔哔声后才能移动到下一个位置。

这是我正在使用的代码:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{

    NSLog(@"(%.02f, %.02f, %.02f)", acceleration.x, acceleration.y, acceleration.z);
    slider.value = acceleration.x;

    if (slider.value == -1)
        [self pushBeep];

    else if (slider.value == 0.00)
        [self pushBap];

    else if (slider.value == 1)
        [self pushBop];

...

这是我的 pushBeep() 方法的代码(仅供参考,pushBeep/pushBap/pushBop 方法都是相同的):

-(void) pushBeep {

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-7" ofType:@"wav"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

    NSError *ierror = nil;
    iPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&ierror];

    [iPlayer play];
}

谁能弄清楚这里有什么问题?

【问题讨论】:

  • 即使您的手机是静态的,加速度计也会不断改变其值。我想这是因为设备对重力很敏感,所以你的哔声会一直持续。
  • 难道我不能在声音到达特定位置后简单地播放声音,然后中断/退出吗?这行得通吗?
  • 为什么不保留变量,因为最后一个声音是哪个值?如果当前值与上一个不同且等于您的触发值之一,则播放声音并保存...
  • 为什么必须使用加速度计值而不是 UIInterfaceOrientation 更改或 UIDeviceOrientation 更改?
  • 我想我明白你在说什么,但你能否澄清一下,以便我完全理解你的建议?

标签: ios avaudioplayer accelerometer


【解决方案1】:

我认为您应该使用内置的方向通知,而不是手动轮询加速度计。如果您需要 FaceUp 和 FaceDown 方向,您可以使用类似下面的方法。或者您可以使用第二种方法来简单地横向、纵向。

第一种方法,依赖于设备方向。如果您需要 FaceUp 或 FaceDown 方向,或者如果您没有 UIViewController,这很重要。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(orientationChanged:)
   name:UIDeviceOrientationDidChangeNotification
   object:[UIDevice currentDevice]];

这是构建的方法。

- (void) orientationChanged:(NSNotification *)note
{
   UIDevice * device = note.object;
   switch(device.orientation)
   {
       case UIDeviceOrientationPortrait:
       /* Play a sound */
       break;

       case UIDeviceOrientationPortraitUpsideDown:
       /* Play a sound */
       break;

       // ....

       default:
       break;
   };
}

第二种方法,它依赖于 UIViewController 的 interfaceOrientations。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            /* Play a Sound */
            break;

        case UIInterfaceOrientationPortrait:
            /* Play a Sound */
            break;

            // .... More Orientations

        default:
            break;
    }
}

【讨论】:

    【解决方案2】:

    加速度计来自加速这个词 - 它不会告诉您设备的方向,它只会告诉您它在空间中的 x、y 和 z 轴移动速度。 使用 UIInterfaceOrientation && UIDeviceOrientation。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2016-07-29
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多