视频录制没有音频?

时间:2017-01-12 16:22:24

标签: ios swift avfoundation

我制作了一个成功的录像节目,但它没有音频?我该怎么做才能解决这个问题?我已在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的解决方案,但它所做的一切都搞砸了我已经使用相机的工作。任何指导表示赞赏!

2 个答案:

答案 0 :(得分:7)

简单的步骤,根据需要实施:

  1. 获取视频捕获设备(AVMediaTypeVideo)
  2. 获取音频捕获设备(AVMediaTypeAudio)
  3. 创建视频输入(带视频设备的AVCaptureDeviceInput)
  4. 创建音频输入(带音频设备的AVCaptureDeviceInput)
  5. 通过beginConfiguration()
  6. 配置捕获会话(AVCaptureSession)
  7. 通过addInput()
  8. 添加输入以捕获会话
  9. 通过commitConfiguration()
  10. 提交配置

答案 1 :(得分:0)

添加音频输入后,如果在一段记录持续时间内仍遇到缺少音频的问题。使用此代码。 它对我有用。

videoOutput?.movieFragmentInterval = .invalid