【问题标题】:Video recording has no Audio?视频录制没有音频?
【发布时间】:2017-01-12 16:22:24
【问题描述】:

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

【问题讨论】:

  • 您的音频会话设置是什么?
  • 我没有任何我知道的音频设置 - 我需要更改吗? @马特

标签: ios swift avfoundation


【解决方案1】:

简单的步骤,随心所欲地实施:

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

【讨论】:

  • 也不要忘记 videoFileOutput.movi​​eFragmentInterval = kCMTimeInvalid
【解决方案2】:

添加音频输入后,如果您在录制一段时间后仍面临音频丢失问题。使用此代码。 它对我有用。

videoOutput?.movieFragmentInterval = .invalid

【讨论】:

    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多