首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >录像没有音频?

录像没有音频?
EN

Stack Overflow用户
提问于 2017-01-12 16:22:24
回答 2查看 2K关注 0票数 7

我做了一个成功的录像程序,但它没有音频?我该怎么做才能解决这个问题?我在我的info.plist中正确设置了音频权限。我正在使用AVCaptureFileOutputRecordingDelegate

代码语言:javascript
复制
 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的解决方案,但是它所做的只是把我已经在用相机工作的东西搞砸了。如有任何指导,将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-12 19:16:06

简单的步骤,以实现您想要的:

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

Stack Overflow用户

发布于 2020-08-19 05:42:36

添加音频输入后,如果您仍然面临一些录音持续时间后丢失的音频问题。用这个代码。对我起作用了。

代码语言:javascript
复制
videoOutput?.movieFragmentInterval = .invalid
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41618162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档