Haptic feedback not playing nice with AVFoundation? (UIImpactFeedbackGenerator, etc)
Asked Answered
T

2

6

I'm trying to have a video/camera view in the background while I also allow for haptic feedback in my app for various actions, but it seems that AVFoundation is not playing nice with any of the calls I am making that involve the haptic calls:

if #available(iOS 10.0, *) {
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.prepare()
    generator.impactOccurred()
    
    // More:

    let feedbackGenerator  = UISelectionFeedbackGenerator()
    feedbackGenerator.selectionChanged()
}

Haptic feedback works great and as expected as long as the AVFoundation stuff is commented out. Any ideas?

Using:

captureSession = AVCaptureSession()

AND:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
Tidal answered 27/5, 2017 at 4:18 Comment(3)
I have same problemAvert
This seems to be causing the audio input (mic) interfering with UIImpactFeedbackGenerator. I am trying to find a way around my self...Barbiturate
Any update on this?Whitaker
I
8

Since iOS 13, you can set setAllowHapticsAndSystemSoundsDuringRecording(_:) for AVAudioSession:

do {
    try AVAudioSession.sharedInstance().setAllowHapticsAndSystemSoundsDuringRecording(true)
} catch {
    print(error)
}

and then you can use:

let generator = UIImpactFeedbackGenerator(style: .light)
generator.prepare()
generator.impactOccurred()
Ijssel answered 21/1, 2021 at 14:16 Comment(2)
The force touch context menus still do not deliver sound despite adding this.Tumor
Seems not working if using the AVCaptureSession and add a audio device as an input.Afterlife
S
2

I assume that if you are using AVCaptureSession then you probably have code like this:

do {
    let audioDevice = AVCaptureDevice.default(for: .audio)
    let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice!)

    if captureSession.canAddInput(audioDeviceInput) {
        captureSession.addInput(audioDeviceInput)
    } else {
        print("Could not add audio device input to the session")
    }
} catch {
    print("Could not create audio device input: \(error)")
}

So audio input is not playing well with haptic engine. You have to remove audio input from capture session before you are playing haptic and then add it back.

Softwood answered 24/4, 2018 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.