【问题标题】:Haptic feedback not playing nice with AVFoundation? (UIImpactFeedbackGenerator, etc)触觉反馈与 AVFoundation 不兼容? (UIImpactFeedbackGenerator 等)
【发布时间】:2017-05-27 04:18:05
【问题描述】:

我正在尝试在后台显示视频/摄像头视图,同时我还允许在我的应用中针对各种操作进行触觉反馈,但似乎 AVFoundation 无法很好地处理我正在进行的任何涉及的调用触觉呼叫:

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

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

只要注释掉 AVFoundation 内容,触觉反馈就可以很好地发挥作用。有什么想法吗?

使用:

captureSession = AVCaptureSession()

与:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

【问题讨论】:

  • 我也有同样的问题
  • 这似乎导致音频输入(麦克风)干扰 UIImpactFeedbackGenerator。我正在努力寻找解决自我的方法...
  • 这有什么更新吗?

标签: ios swift avfoundation avcapturedevice haptic-feedback


【解决方案1】:

从iOS 13开始,可以将setAllowHapticsAndSystemSoundsDuringRecording(_:)设置为AVAudioSession

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

然后你可以使用:

let generator = UIImpactFeedbackGenerator(style: .light)
generator.prepare()
generator.impactOccurred()

【讨论】:

    【解决方案2】:

    我假设如果您使用的是 AVCaptureSession,那么您可能有这样的代码:

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

    因此,触觉引擎无法很好地播放音频输入。在播放触觉之前,您必须从捕获会话中删除音频输入,然后再将其添加回来。

    【讨论】:

      猜你喜欢
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多