Я пытаюсь получить доступ к среднему аудиоканалу с помощью AVCaptureAudioDataOutput, а затем с помощью установки setSampleBufferDelegate. Тем не менее, делегат вообще не вызывается. Что мне не хватает? fun c setUpCaptureSession () {
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSession.Preset.high
// Add inputs
let camera = bestCamera()
// Video
guard let captureInput = try? AVCaptureDeviceInput(device: camera),
captureSession.canAddInput(captureInput) else {
return
}
captureSession.addInput(captureInput)
if captureSession.canSetSessionPreset(.high) {
captureSession.sessionPreset = .high
}
if captureSession.canAddOutput(cameraOutput){
captureSession.addOutput(cameraOutput)
}
// Add outputs
let microphone = bestAudio()
guard let audioInput = try? AVCaptureDeviceInput(device: microphone),
captureSession.canAddInput(audioInput) else {
return
}
captureSession.addInput(audioInput)
self.audioInput = audioInput
audioDataOutput = AVCaptureAudioDataOutput()
guard captureSession.canAddOutput(audioDataOutput) else {
return
}
audioDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
captureSession.addOutput(audioDataOutput)
print("videodataoutput added")
// Recording to disk
guard captureSession.canAddOutput(fileOutput) else {
return
}
captureSession.addOutput(fileOutput)
captureSession.commitConfiguration()
}
Здесь я вызываю делегата:
extension CameraController: AVCaptureAudioDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
if output == audioDataOutput {
print("AUDIO OUTPUT")
DispatchQueue.main.async {
print("DELEGATE")
let channel = connection.audioChannels[1];
let averagePowerLevel = channel.averagePowerLevel
print("AVERAGE POWER: \(averagePowerLevel)")
}
}
}
}
Все остальное работает, но AVCaptureAudioDataOutputSampleBufferDelegate никогда не вызывается.