AVCaptureSession Record Video With Audio

后端 未结 3 1608
孤独总比滥情好
孤独总比滥情好 2021-02-01 05:48

I have my app set up to record video from the camera using an AVCaptureSession, however, there is no audio with it. What do I need to do to record audio and then add it to the

相关标签:
3条回答
  • 2021-02-01 06:01

    Add below code between beginConfiguration() and commitConfiguration()

    // Add audio device to the recording
    
    let audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
    do {
        let audioInput = try AVCaptureDeviceInput(device: audioDevice)
        self.captureSession.addInput(audioInput)
    } catch {
        print("Unable to add audio device to the recording.")
    }
    
    0 讨论(0)
  • 2021-02-01 06:05

    in swift 5x, you can use this:

    do {
            guard let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) else {
                print("Default audio device is unavailable.")
                setupResult = .configurationFailed
                session.commitConfiguration()
                return
            }
    
            // Add audio input
            let audioInput = try AVCaptureDeviceInput(device: audioDevice)
            if session.canAddInput(audioInput) {
                session.addInput(audioInput)
            } else {
                print("Couldn't add audio device input to the session.")
                setupResult = .configurationFailed
                session.commitConfiguration()
                return
            }
    
        } catch {
            print("Couldn't create Audio device input: \(error)")
            setupResult = .configurationFailed
            session.commitConfiguration()
            return
        }
    
    0 讨论(0)
  • 2021-02-01 06:23

    You have not included the audio device:

    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
    [session addInput:audioInput]
    

    between beginConfiguration and commitConfiguration. It'll work!!!

    0 讨论(0)
提交回复
热议问题