iOS中是否有用于自定义振动的API?

时间:2012-10-19 02:18:06

标签: ios vibrate iphone-vibrate

从iOS 5开始,用户可以为警报和响铃创建自定义振动模式。以下屏幕截图显示了用于创建一个UI(iOS 6中的联系人应用程序)的用户界面:

Screenshot of UI for creating custom vibrations in iOS 6

我一直在搜索,包括文档,我找不到任何公开的API来揭示自定义振动的创建或回放。最接近的是使用AudioToolbox框架来发挥短暂的持续振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

有没有人知道是否有自定义振动的API?它不一定是公共API。我很想知道联系人应用程序使用的是什么。有谁知道吗?

P.S。其他人在_CTServerConnectionCreateexample)中建议CoreTelephony。我试过了,但由于某种原因无法振动。

  

2015年10月更新:

     

iOS 9中有新的私有API可与兼容设备中的Taptic Engine进行交互。有关详情,请参阅Taptic in iOS 9

2 个答案:

答案 0 :(得分:100)

经过一段时间的搜索联系应用程序后,我已经弄清楚它是如何工作的。

ABNewPersonViewControlle调用ToneLibrary框架中的一些类来执行此操作。

调用堆栈如下所示:

0   CoreFoundation                  0x3359a1d4 CFGetTypeID + 0
1   CoreFoundation                  0x33596396 __CFPropertyListIsValidAux + 46
2   CoreFoundation                  0x33517090 CFPropertyListCreateData + 124
3   AudioToolbox                    0x38ac255a AudioServicesPlaySystemSoundWithVibration + 158
5   ToneLibrary                     0x35a7811a -[TLVibrationRecorderView vibrationComponentDidStartForVibrationRecorderTouchSurface:] + 38
6   ToneLibrary                     0x35a772b2 -[TLVibrationRecorderTouchSurface touchesBegan:withEvent:] + 342
7   UIKit                           0x3610f526 -[UIWindow _sendTouchesForEvent:] + 314
8   UIKit                           0x360fc804 -[UIApplication sendEvent:] + 376

在网上搜索“AudioServicesPlaySystemSoundWithVibration”后,我什么也没找到。

所以我决定亲自调查一下。它是AudioToolbox框架中的私有函数。

该功能的声明就像

void AudioServicesPlaySystemSoundWithVibration(SystemSoundID inSystemSoundID,id arg,NSDictionary* vibratePattern)

“inSystemSoundID”是SystemSoundID。就像“AudioServicesPlaySystemSound”一样,传递“kSystemSoundID_Vibrate”。

“arg”并不重要,传递给它,一切都会正常工作。

“vibratePattern”是“NSDictionary”的指针,Contact App传入     {     强度= 1;     OffDuration = 1;     OnDuration = 10;     用于记录用户输入。

但只有调用此功能才能使振动永不停止。所以我必须找到一些功能来阻止它。

答案是“AudioServicesStopSystemSound”。它也是AudioToolbox框架中的私有函数。

该功能的声明就像

void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)

我猜联系应用程序在touchesBegan方法中使用AudioServicesPlaySystemSoundWithVibration,而在touchEnd方法中使用AudioServicesStopSystemSound来达到此效果。

TLVibrationController将管理一个振动模式对象来记录您输入的过程。

最后生成一个字典,传入AudioServicesPlaySystemSoundWithVibration以重放整个过程,如下所示:

NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];

[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms
[arr addObject:[NSNumber numberWithInt:2000]];

[arr addObject:[NSNumber numberWithBool:NO]];  //stop for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];

[arr addObject:[NSNumber numberWithBool:YES]];  //vibrate for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];

[arr addObject:[NSNumber numberWithBool:NO]];    //stop for 500ms
[arr addObject:[NSNumber numberWithInt:500]];

[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"Intensity"];


AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);

因此,如果您想在iOS中进行自定义振动。使用AudioServicesPlaySystemSoundWithVibration和AudioServicesStopSystemSound。

答案 1 :(得分:3)

有一个名为HapticKeyboard的项目可以做到这一点。请参阅:http://code.google.com/p/zataangstuff/source/browse/trunk/HapticKeyboard/Classes/HapticKeyboard.m?r=93

具体来说,看一下最后一组功能

_CTServerConnectionSetVibratorState(&x, connection, 0, 0, 0, 0, 0);

有两个问题:

  1. 我怀疑你是否允许你的应用进入应用程序商店。 Apple会将此视为用户的电池消耗,并且他们对用户体验非常严格
  2. 这可能需要越狱。自从我上次玩这个以来,有几个新的iOS版本,所以很难说