iOS中是否有用于自定义振动的API? [英] Are there APIs for custom vibrations in iOS?

查看:527
本文介绍了iOS中是否有用于自定义振动的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Starting in iOS 5, users are able to create custom vibration patterns for alerts and rings. The following screenshot shows the UI for creating one (Contacts app in iOS 6):

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

I've been searching around, including the documentation, and I cannot find any public APIs that expose the creation or playback of custom vibrations. The closest thing is to use the AudioToolbox framework to play a short, constant vibration:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

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

Does anyone know if there are APIs for custom vibrations? It doesn't necessarily have to be public APIs. I'm curious to know what the Contacts app uses. Does anyone know?

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

P.S. Others have suggested _CTServerConnectionCreate in CoreTelephony (example). I tried it, but couldn't get any vibration going for some reason.


2015年10月更新:

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

There are new private APIs in iOS 9 to interact with the Taptic Engine in compatible devices. See Taptic in iOS 9 for more.


推荐答案

在搜索联系人应用程序之后,我已经弄清楚它是如何工作的。

After serval hours' digging in the Contact App, I have figured out how it works.

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

ABNewPersonViewControlle invoke some class in ToneLibrary framework to do this.

调用堆栈如下所示:

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之后,我一无所获。

After search "AudioServicesPlaySystemSoundWithVibration" on the web , I found nothing.

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

So I decide to look into it myself. It's a private function in AudioToolbox framework.

该函数的声明类似于

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

inSystemSoundIDis SystemSoundID。就像AudioServicesPlaySystemSound一样,传递kSystemSoundID_Vibrate。

"inSystemSoundID" is SystemSoundID .just like "AudioServicesPlaySystemSound", pass "kSystemSoundID_Vibrate".

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

"arg" is not important, pass nil to it , everything will still work fine.

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

"vibratePattern" is a pointer of "NSDictionary", the Contact App pass into { Intensity = 1; OffDuration = 1; OnDuration = 10; } for recording user input.

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

But only call this function will make a vibration never stop. So I have to found some function to stop it.

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

The answer is "AudioServicesStopSystemSound". It's also a private function in AudioToolbox framework.

函数的声明就像

void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)

我猜联系人应用程序在touchesBegan方法中使用AudioServicesPlaySystemSoundWithVibration和touchSernd方法中的AudioServicesStopSystemSound达到此效果。

I guess the Contact App use AudioServicesPlaySystemSoundWithVibration in touchesBegan method, and AudioServicesStopSystemSound in touchEnd method to reach this effect.

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

TLVibrationController will manager a vibrate pattern object to record the process you input.

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

At last it generate a dictionary to pass into AudioServicesPlaySystemSoundWithVibration to replay the whole process like below:

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。

So if you want a custom vibrations in iOS. Use AudioServicesPlaySystemSoundWithVibration and AudioServicesStopSystemSound.

这篇关于iOS中是否有用于自定义振动的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆