我正在努力解決一個問題:我有預覽層的AVCaptureSession,並且我還想在用戶點擊按鈕時提供觸覺反饋。如果我將audioInput添加到我的AVCaptureSession中,那麼我根本無法生成觸覺反饋。我試圖在開始記錄之前添加audioInput,並在停止後立即刪除,但修改捕獲會話配置(我在串行隊列中進行的操作)會導致視頻預覽打嗝(它會在幾分之一秒內中斷)。我仍然不知道Snapchat和Instagram如何操作這個技巧。我的猜測之一是,他們以某種方式配置AVAudioSession,但我無法弄清楚。只有在不中斷視頻預覽的情況下進行錄製時,如何將音頻輸入添加到AVCaptureSession?
我捕獲會話初始化是相當普遍的,所以我不會粘貼(這裏值得注意的是,我有captureSession.automaticallyConfiguresApplicationAudioSession = false
線和共享AVAudioSession被修改,像這樣AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVideoRecording, options: [.mixWithOthers])
),但我會後我試圖切換音頻輸入:
func addAudioInput() {
self.sessionQueue.async { [unowned self] in
self.captureSession.beginConfiguration()
let microphone = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
if let audioInput = try? AVCaptureDeviceInput(device: microphone), self.captureSession.canAddInput(audioInput) {
self.captureSession.addInput(audioInput)
}
self.captureSession.commitConfiguration()
}
}
func removeAudioInput() {
self.sessionQueue.async { [unowned self] in
if let audioInput = self.captureSession.inputs.first(where: { ($0 as? AVCaptureDeviceInput)?.device.deviceType == .builtInMicrophone }) as? AVCaptureDeviceInput {
self.captureSession.beginConfiguration()
self.captureSession.removeInput(audioInput)
self.captureSession.commitConfiguration()
}
}
}
如果你有代碼示例請更新答案哥們 –