我制作了一个成功的录像节目,但它没有音频?我该怎么做才能解决这个问题?我已在info.plist
中正确设置了音频权限。我正在使用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")
}
}
}
我已经尝试过发布here的解决方案,但它所做的一切都搞砸了我已经使用相机的工作。任何指导表示赞赏!
答案 0 :(得分:7)
简单的步骤,根据需要实施:
答案 1 :(得分:0)
添加音频输入后,如果在一段记录持续时间内仍遇到缺少音频的问题。使用此代码。 它对我有用。
videoOutput?.movieFragmentInterval = .invalid