简体   繁体   中英

iOS AVAudioRecorder.record() returns false on device

AVAudioRecorder.record() returns true only first time after granting permission to microphone with system alert, than it always return false . On simulator .record() always return true . How to make AVAudioRecordenr record on the second and others app launch?

Here is the code:

func startRecording() {
        let dirPath =  NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] as String
        let recordName = "record.wav"
        self.audioURL = URL(fileURLWithPath: dirPath + "/" + recordName)
        let session = AVAudioSession.sharedInstance()
        let microPhoneStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
        print(microPhoneStatus)
            do {
                try session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.duckOthers)
            } catch {
                print(error)
            }

            guard let recorder  = try? AVAudioRecorder(url: self.audioURL, settings: [:]) else {
                self.finishRecording(success: false)
                return
            }


            self.audioRecorder = recorder
            self.audioRecorder.isMeteringEnabled = true
            self.audioRecorder.delegate = self
            let prepare = self.audioRecorder.prepareToRecord()
            try! session.setActive(true, options: [])
            let record = self.audioRecorder.record()
    }

Already tried adding NSMicrophoneUsageDescription and requesting AVAudioSession requestRecordPermission from code:

Two important things need to be done before recording audio.

  1. The app needs to declare why/how recording is necessary. In Info.plist add a key for Privacy - Microphone Usage Description with a good textual description.

  2. The app needs to request explicit user permission before each call to AVAudioRecorder.record() . Implement something resembling:

import AVFoundation

let session = AVAudioSession.sharedInstance()
session.setCategory(.playAndRecord, mode: .default)
session.setActive(true)
session.requestRecordPermission { granted in
    print(granted)
}

Note that this is a simplified sample. Additional do/try/catch error handling will need to be added as these methods are marked throws .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM