'Core Haptics not playing in iOS 15 with certain AVAudioSession configurations

I'm using Core Haptics to play a custom "heart beat" pattern, while simultaneously playing music using AVAudioPlayer. Specifically, I'm creating a simple CHHapticPattern, then creating a CHHapticEngine to play that pattern.

The custom haptics play just fine on an iPhone running iOS 14, but completely stopped working in iOS 15.0+. The haptic engine doesn't throw any errors in iOS 15, it just fails silently. Standard haptic feedback such as UIImpactFeedbackGenerator work just fine; it's just the custom haptics that fail.

After lots of debugging and seeing no obvious reasons why Core Haptics should suddenly stop working, I eventually discovered the culprit is that I have an AVAudioSession configured as follows:

     // Setup AVAudioSession
        do {
            
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.duckOthers]) // <-- This conflicts with Core Haptics on iOS 15, but not iOS 14
            try AVAudioSession.sharedInstance().setActive(true)
            
        } catch {
            print("Error activating AVAudioSession: \(error)")
        }

If I remove the .duckOthers option (e.g., replace with .mixWithOthers), then the custom haptic pattern plays successfully in iOS 15 (and 14).

While I can live with using .mixWithOthers instead of .duckOthers, I can't see any obvious rationale why .duckOthers should conflict with Core Haptics, and suddenly start conflicting with it only in iOS 15.0+. Any ideas why? Otherwise I will file a bug report with Apple.

Here is a link to a stripped down, demo project demonstrating the issue. If you run it on iOS 15 on an iPhone that supports haptics (iPhone 8 or later), the custom haptic pattern doesn't play, but it plays just fine on iOS 14: link to project



Solution 1:[1]

You can set up your CHHapticEngine and set the property of .playsHapticsOnly to yes to avoid conflicting with AVAudioSession.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hzzz