如何将iPhone设置为震动一次?

例如,当玩家失去一条生命或游戏结束时,iPhone应该震动。


当前回答

在我的旅行中,我发现如果你在录制音频时尝试以下任何一种方法,即使启用了它,设备也不会振动。

1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

我的方法在测量设备运动的特定时间被调用。我不得不停止录音,然后在震动发生后重新开始录音。

它是这样的。

-(void)vibrate {
    [recorder stop];
    AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    [recorder start];
}

recorder是一个AVRecorder实例。

希望这能帮助到以前有同样问题的人。

其他回答

对于以某种方式关闭振动的设备,我遇到了很大的麻烦,但我们无论如何都需要它工作,因为它对我们的应用程序功能至关重要,而且由于它只是文档方法调用的整数,因此它将通过验证。所以我已经尝试了一些在TUNER88/ iossystemsoundlibrary之外的声音

然后我偶然发现了1352,不管静音开关或设备上的设置(设置->震动铃声,震动静音),它都在工作。

- (void)vibratePhone;
{
     if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
     {
         AudioServicesPlaySystemSound (1352); //works ALWAYS as of this post
     }
     else
     {
          // Not an iPhone, so doesn't have vibrate
          // play the less annoying tick noise or one of your own
          AudioServicesPlayAlertSound (1105);
     }
}

如果您正在使用Xamarin (monotouch)框架,只需调用

SystemSound.Vibrate.PlayAlertSound()

在我的例子中,我使用的是AVCaptureSession。 AudioToolbox是在项目的构建阶段,它被导入,但仍然不能工作。为了使它工作,我在振动之前停止会话,然后继续。

#import <AudioToolbox/AudioToolbox.h>
...
@property (nonatomic) AVCaptureSession *session;
...
- (void)vibratePhone;
{
  [self.session stopRunning];
     NSLog(@"vibratePhone %@",@"here");
    if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
    {
        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); 
    }
    else
    {
        AudioServicesPlayAlertSound (kSystemSoundID_Vibrate);
    }
  [self.session startRunning];
}

重要提示:未来弃用警报。 从iOS 9.0开始,API函数描述如下:

AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID)
AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID)

包括以下说明:

This function will be deprecated in a future release.
Use AudioServicesPlayAlertSoundWithCompletion or  
AudioServicesPlaySystemSoundWithCompletion instead.

正确的方法是使用以下两种:

AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate, nil)

or

AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate) {
 //your callback code when the vibration is done (it may not vibrate in iPod, but this callback will be always called)
}

记得导入AVFoundation

在我的旅行中,我发现如果你在录制音频时尝试以下任何一种方法,即使启用了它,设备也不会振动。

1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

我的方法在测量设备运动的特定时间被调用。我不得不停止录音,然后在震动发生后重新开始录音。

它是这样的。

-(void)vibrate {
    [recorder stop];
    AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    [recorder start];
}

recorder是一个AVRecorder实例。

希望这能帮助到以前有同样问题的人。