Does the video have no audio?

I made a successful video recording program, but it has no sound? What should I do to fix this? I have permissions correctly configured in my info.plist . I am using 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") } } } 

I tried the solution posted here , but all he did was spoil what I already worked with the camera. Any guidance appreciated!

+6
source share
1 answer

Simple steps to implement as you want:

  • Get your video capture device (AVMediaTypeVideo).
  • Get your capture device for audio (AVMediaTypeAudio)
  • Create your own video input (AVCaptureDeviceInput with a video device)
  • Create your audio input (AVCaptureDeviceInput with sound device)
  • Configure a capture session (AVCaptureSession) via beginConfiguration ()
  • Add capture inputs with addInput ()
  • Commit configuration using commitConfiguration ()
+6
source

Source: https://habr.com/ru/post/1014020/


All Articles