UIImpactFeedbackGenerator Not Working When Audio Device Added to AVCaptureSession

怎甘沉沦 提交于 2020-12-29 03:01:06

问题


Adding microphone audio input to AVCaptureSession seems to disable UIImpactFeedbackGenerator.

let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice)
if self.session.canAddInput(audioDeviceInput) {
   self.session.addInput(audioDeviceInput)
}

Once the audio device is removed, feedback resumes.

Is this normal behavior? Is there a way around this?

I notice stock iOS Camera app in video mode and long pressing on thumbnail seem to still make the feedback work. So there has to be a way around this?


回答1:


This seems to be an intentional behavior.

You can stop the capture session, play the haptic, then resume the capture session, which appears to be what the camera app is doing, as when you peek the camera feed goes to a blurred static image of the last frame captured. For example:

self.session.stopRunning()
// Play haptic
UINotificationFeedbackGenerator().notificationOccurred(.warning)
// Not completely sure if this delay is needed but you might need to wait until the run loop after the haptic plays to resume capture session
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
{

    self.session.startRunning()
}



回答2:


Audio session (via AVAudioPlayer, AVCaptureMovieFileOutput, etc), then the phone will NOT vibrate.

I am not sure but 99% not working with AVCaptureMovieFileOutput. Please more info: https://developer.apple.com/documentation/audiotoolbox/1405202-audioservicesplayalertsound



来源:https://stackoverflow.com/questions/46750361/uiimpactfeedbackgenerator-not-working-when-audio-device-added-to-avcapturesessio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!