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!
You might have turned down the sound and set the device to silent mode for any reason. The phone, therefore, has no sound once you play the video. This can lead to a problem and you might think that the device is out of order when it is not. Turn on the sound from the side button and you are done.
Quit and Reopen the Camera AppThe camera app may have a glitch leading to audio not being captured when recording videos. You can try closing the camera app, killing it from the recent apps list, and then launching it again to see if it resolves the issue.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With