73

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

在 iOS 6 中创建自定义振动的 UI 截图

我一直在四处搜索,包括文档,但找不到任何公开自定义振动的创建或播放的公共 API。最接近的是使用AudioToolbox框架来播放一个短暂的、持续的振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

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

PS其他人_CTServerConnectionCreateCoreTelephony示例)中提出了建议。我试过了,但由于某种原因无法振动。

2015 年 10 月更新:

iOS 9 中有新的私有 API 可以与兼容设备中的 Taptic Engine 进行交互。有关更多信息,请参阅iOS 9 中的 Taptic

4

2 回答 2

104

在联系人应用程序中挖掘了几个小时后,我已经弄清楚它是如何工作的。

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”并不重要,将 nil 传递给它,一切都会正常工作。

"vibratePattern" 是 "NSDictionary" 的指针,Contact App 传入 { Intensity = 1; 关闭持续时间 = 1;持续时间 = 10;} 用于记录用户输入。

但是只有调用这个函数才会让振动永不停止。所以我必须找到一些功能来阻止它。

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

函数的声明就像

void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)

我猜Contact App在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。

于 2012-10-24T10:34:50.680 回答
4

有一个名为 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 版本,所以很难说
于 2012-10-23T14:25:40.313 回答