I have made a successful video recording program, but it has no audio? What should I do to fix this? I have the audio permissions properly set up in my info.plist
. I am using the AVCaptureFileOutputRecordingDelegate
.
override func viewWillAppear(_ animated: Bool) {
let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)
for device in (deviceDiscoverySession?.devices)!{
if(device.position == AVCaptureDevicePosition.back){
do{
let input = try AVCaptureDeviceInput(device: device )
if captureSession.canAddInput(input){
captureSession.addInput(input)
}
// sessionOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange as UInt32)]
sessionOutput.alwaysDiscardsLateVideoFrames = true
if(captureSession.canAddOutput(sessionOutput) == true){
captureSession.addOutput(sessionOutput)
let previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)
preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
preview?.position = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
preview?.videoGravity = AVLayerVideoGravityResize
return preview!
}()
view.layer.insertSublayer(previewLayer, at: 0)
output = AVCaptureMovieFileOutput()
let maxDuration = CMTimeMakeWithSeconds(180, 30)
output.maxRecordedDuration = maxDuration
captureSession.addOutput(output)
}
captureSession.commitConfiguration()
}
catch{
print("Error")
}
}
}
I have tried the solution posted here but all it did was mess up what I already had working with the camera. Any guidance is appreciated!
Simple steps, to implement as you want:
After adding audio input if you are still facing missing audio issues after some record duration. use this code. It worked for me.
videoOutput?.movieFragmentInterval = .invalid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.