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
: