Save Video Using AVFoundation Swift

后端 未结 6 1545
感情败类
感情败类 2021-02-05 08:57

Hi I followed a course by Jared Davidson to create a custom camera view and save pictures using AVFoundation. https://www.youtube.com/watch?v=w0O3ZGUS3pk

However I would

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 09:21

    For the sound recording issue,

    Add this code when creating the captureSession

    askMicroPhonePermission(completion: { (isMicrophonePermissionGiven) in

                if isMicrophonePermissionGiven {
                    do {
                        try self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureAudio))
                    } catch {
                        print("Error creating the database")
                    }
                }
            })
    

    ////////////////////////////////////////////////////////////////

    askMicroPhonePermission function is as follows

    func askMicroPhonePermission(completion: @escaping (_ success: Bool)-> Void) {
        switch AVAudioSession.sharedInstance().recordPermission() {
        case AVAudioSessionRecordPermission.granted:
            completion(true)
        case AVAudioSessionRecordPermission.denied:
            completion(false) //show alert if required
        case AVAudioSessionRecordPermission.undetermined:
            AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
                if granted {
                    completion(true)
                } else {
                    completion(false) // show alert if required
                }
            })
        default:
            completion(false)
        }
    }
    

    And you have to add NSMicrophoneUsageDescription key value in info.plist file.

提交回复
热议问题