为什么 Core Haptics 不播放? (CHHapticPatternPlayer)

Why doesn't Core Haptics play? (CHHapticPatternPlayer)

我在物理设备 (iPhone XR) 上测试以下代码时没有触觉输出。我遵循了 Apple Developer 的“Playing a Single-tap Haptic Pattern”文章,并仔细检查了互联网上的各种其他文章,我确信我已经正确实施了它。此外,在 运行 时没有捕获到错误。我的设备不输出触觉模式的原因可能是什么?

旁注:

  1. 我可以确认我的 phone 确实为其他应用程序产生了触觉,所以这似乎不是我的物理设备的问题。
  2. 我也单独实现了一个AVAudioPlayer;我怀疑这会干扰,但我想我会提到它以防万一。

任何帮助将不胜感激 -- 谢谢!

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)
}

问题出在这一行:

hapticEngine.stop(completionHandler: nil)

删掉就好了。你在它试图开始的那一刻就停止了你的“声音”。这没有多大意义。

(当然,我还假设某处有实际调用您的 playHaptic 方法的代码。您没有显示该代码,但我只是猜测您可能记得包含一些代码。确保发生这种情况后,我 运行 您的带有 stop 行的代码被注释掉了,我确实感觉到并听到了轻敲的“啪”声。)