iOS AVAudioRecorder.record()在设备上返回false

时间:2018-09-11 15:28:05

标签: ios avfoundation avaudiorecorder

AVAudioRecorder.record()仅在向具有系统警报的麦克风授予权限后才第一次返回true,而不是总是返回false 。在模拟器.record()上始终返回true。 如何在第二个和其他应用程序启动时使AVAudioRecordenr记录?

这是代码:

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()
    }

已经尝试通过代码添加NSMicrophoneUsageDescription并请求AVAudioSession requestRecordPermission

0 个答案:

没有答案