iOS 5 以降では、ユーザーはアラートや着信音のカスタム振動パターンを作成できます。次のスクリーンショットは、カスタム振動パターンを作成するための UI を示しています (iOS 6 の連絡先アプリ)。
ドキュメントも含めていろいろ調べてみましたが、カスタム振動の作成や再生を公開している公開 API は見つかりませんでした。最も近いのは、フレームワークを使用してAudioToolbox
短く一定の振動を再生することです。
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
カスタム振動用の API があるかどうか知っている人はいますか?必ずしもパブリック API である必要はありません。連絡先アプリが何を使用しているか知りたいです。誰か知っていますか?
PS 他の人が提案して_CTServerConnectionCreate
いるCoreTelephony
(例)。試してみましたが、なぜか振動しませんでした。
2015年10月更新:
iOS 9には、互換性のあるデバイスのTaptic Engineとやり取りするための新しいプライベートAPIがあります。iOS 9のTaptic多くのための。
ベストアンサー1
連絡先アプリを数時間調べた後、その仕組みが分かりました。
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」へのポインターであり、連絡先アプリはユーザー入力を記録するために { Intensity = 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 を使用します。