Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video recording has no Audio?

I have made a successful video recording program, but it has no audio? What should I do to fix this? I have the audio permissions properly set up in my info.plist. I am using the 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 have tried the solution posted here but all it did was mess up what I already had working with the camera. Any guidance is appreciated!

like image 820
Mike Schmidt Avatar asked Jan 12 '17 16:01

Mike Schmidt


People also ask

Why is there no sound in my video recording?

You might have turned down the sound and set the device to silent mode for any reason. The phone, therefore, has no sound once you play the video. This can lead to a problem and you might think that the device is out of order when it is not. Turn on the sound from the side button and you are done.

Why is there no sound on my iPhone when I record a video?

Quit and Reopen the Camera AppThe camera app may have a glitch leading to audio not being captured when recording videos. You can try closing the camera app, killing it from the recent apps list, and then launching it again to see if it resolves the issue.


2 Answers

Simple steps, to implement as you want:

  1. Get your capture device for video (AVMediaTypeVideo)
  2. Get your capture device for audio (AVMediaTypeAudio)
  3. Create your video input (AVCaptureDeviceInput with video device)
  4. Create your audio input (AVCaptureDeviceInput with audio device)
  5. Configure your capture session (AVCaptureSession) via beginConfiguration()
  6. Add Inputs to capture session via addInput()
  7. Commit configuration via commitConfiguration()
like image 146
Florent Morin Avatar answered Oct 15 '22 04:10

Florent Morin


After adding audio input if you are still facing missing audio issues after some record duration. use this code. It worked for me.

videoOutput?.movieFragmentInterval = .invalid
like image 1
Faris Muhammed Avatar answered Oct 15 '22 04:10

Faris Muhammed