ios - 录像没有声音?

标签 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 的解决方案但它所做的只是搞乱了我已经用相机工作的东西。任何指导表示赞赏!

最佳答案

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

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

关于ios - 录像没有声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618162/

相关文章:

ios - 如何在iOS中生成24位True Color动画Gif?

ios - 使用 AudioKit 的量化序列

objective-c - 任何人都有如何使用 AVAssetWriter 同时编写音频和视频的示例?

ios - 核心数据可以用来保存可编码结构吗?

xcode - AVFoundation 框架中不存在 AVAudioSession

ios - 当应用程序在后台时,AVAssetReader 将无法正确初始化,因此无法开始阅读

ios - 不要在跨平台 iOS/OSX 模块上的 OSX 代码中编译 iOS

ios - 在 UINavigationBar 上设置导航项时崩溃

ios - 在Swift 3中弹出模态

ios - 如何根据为 nil 的特定属性删除对象