-
Notifications
You must be signed in to change notification settings - Fork 523
Feat/ios modern vibration api #2247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
|
|
||
| #import <AudioToolbox/AudioServices.h> | ||
| #import <AVFoundation/AVFoundation.h> | ||
| #import <CoreHaptics/CoreHaptics.h> | ||
|
|
||
| #include "modules/audio/Audio.h" | ||
|
|
||
|
|
@@ -364,10 +365,57 @@ - (void)applicationBecameActive:(NSNotification *)note | |
| return path; | ||
| } | ||
|
|
||
| void vibrate() | ||
| void vibrate(double seconds) | ||
| { | ||
| @autoreleasepool | ||
| { | ||
| if (@available(iOS 13.0, *)) | ||
| { | ||
| NSError *error = nil; | ||
| CHHapticEngine *engine = [[CHHapticEngine alloc] | ||
| initAndReturnError:&error]; | ||
|
|
||
| if (engine != nil) | ||
| { | ||
| [engine startAndReturnError:nil]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apple's docs say "This method blocks all subsequent event processing on the current thread until the engine has started." and they have an alternative async method. What sort of performance cost does using the sync method have versus the async one? |
||
|
|
||
| NSDictionary *hapticDict = @{ | ||
| CHHapticPatternKeyEvent: @[@{ | ||
| CHHapticPatternKeyEventType: | ||
| CHHapticEventTypeHapticContinuous, | ||
| CHHapticPatternKeyTime: @0.0, | ||
| CHHapticPatternKeyEventDuration: @(seconds), | ||
| CHHapticPatternKeyEventParameters: @[ | ||
| // Correlates to Android's default high intensity vibration. | ||
| @{ | ||
| CHHapticPatternKeyParameterID: | ||
| CHHapticEventParameterIDHapticIntensity, | ||
| CHHapticPatternKeyParameterValue: @1.0 | ||
| }, | ||
| // Medium sharpness, 0.0 is dull rumble while 1.0 is crisp tap. | ||
| @{ | ||
| CHHapticPatternKeyParameterID: | ||
| CHHapticEventParameterIDHapticSharpness, | ||
| CHHapticPatternKeyParameterValue: @0.5 | ||
| } | ||
| ] | ||
| }] | ||
| }; | ||
|
|
||
| CHHapticPattern *pattern = [[CHHapticPattern alloc] | ||
| initWithDictionary:hapticDict error:nil]; | ||
|
|
||
| if (pattern != nil) | ||
| { | ||
| id<CHHapticPatternPlayer> player = | ||
| [engine createPlayerWithPattern:pattern error:nil]; | ||
| [player startAtTime:0 error:nil]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (and earlier code) should probably have some form of error checking |
||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Fallback: iOS < 13 or Core Haptics unavailable. | ||
| AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apple's haptic engine docs say to "Maintain a strong reference to it so it doesn’t go out of scope while the haptic is playing."
Should this code be changed to do that?