I am experiencing no haptic output while testing the code below on my physical device (iPhone XR). I followed Apple Developer's "Playing a Single-tap Haptic Pattern" article, as well as double-checked with various other articles on the internet, and I am confident I have implemented it correctly. Moreover, there are no errors that are caught when running. What could be the reason why my device is not outputting the haptic pattern?
Side Notes:
Any help would be much appreciated--thanks!
var hapticEngine: CHHapticEngine!
var hapticPlayer: CHHapticPatternPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Create and configure a haptic engine.
do {
hapticEngine = try CHHapticEngine()
} catch let error {
fatalError("Engine Creation Error: \(error)")
}
// The reset handler provides an opportunity to restart the engine.
hapticEngine.resetHandler = {
print("Reset Handler: Restarting the engine.")
do {
// Try restarting the engine.
try self.hapticEngine.start()
// Register any custom resources you had registered, using registerAudioResource.
// Recreate all haptic pattern players you had created, using createPlayer.
} catch let error {
fatalError("Failed to restart the engine: \(error.localizedDescription)")
}
}
// The stopped handler alerts engine stoppage.
hapticEngine.stoppedHandler = { reason in
print("Stop Handler: The engine stopped for reason: \(reason.rawValue)")
switch reason {
case .audioSessionInterrupt: print("Audio session interrupt")
case .applicationSuspended: print("Application suspended")
case .idleTimeout: print("Idle timeout")
case .systemError: print("System error")
@unknown default:
print("Unknown error")
}
}
// Create haptic dictionary
let hapticDict = [
CHHapticPattern.Key.pattern: [
[
CHHapticPattern.Key.event: [
CHHapticPattern.Key.eventType: CHHapticEvent.EventType.hapticTransient,
CHHapticPattern.Key.time: 0.001,
CHHapticPattern.Key.eventDuration: 1.0
]
]
]
]
// Create haptic pattern from haptic dictionary, then add it to the haptic player
do {
let pattern = try CHHapticPattern(dictionary: hapticDict)
hapticPlayer = try hapticEngine.makePlayer(with: pattern)
} catch let error {
print("Failed to create hapticPlayer: \(error.localizedDescription)")
}
// ...
}
// ...
func playHaptic() {
//audioPlayer?.play()
// Start Haptic Engine
do {
try hapticEngine.start()
} catch let error {
print("Haptic Engine could not start: \(error.localizedDescription)")
}
// Start Haptic Player
do {
try hapticPlayer.start(atTime: 0.0)
print("Why")
} catch let error {
print("Haptic Player could not start: \(error.localizedDescription)")
}
// Stop Haptic Engine
hapticEngine.stop(completionHandler: nil)
}
The problem is this line:
hapticEngine.stop(completionHandler: nil)
Delete it and all will be well. You're stopping your "sound" the very instant it is trying to get started. That doesn't make much sense.
(Of course I am also assuming that somewhere there is code that actually calls your playHaptic
method. You didn't show that code, but I'm just guessing that you probably remembered to include some. Having made sure that happens, I ran your code with the stop
line commented out, and I definitely felt and heard the "pop" of the taptic tap.)