此代码在swift 2.3中的其他项目中运行良好。但是当我试图在swift 3.0中实现它时,我得到了EXC_BAD_ACCESS。在
audioRecorder = try AVAudioRecorder(url: self.directoryURL() as! URL , settings: settings)
代码:
func setUpAudioRecording()
{
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setActive(true)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
audioRecorder = try AVAudioRecorder(url: self.directoryURL() as! URL , settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
}
catch let error
{
print(error.localizedDescription)
}
}
func directoryURL() -> NSURL? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let audioURL = url.appendingPathComponent("recording.3gp")
return audioURL as NSURL?
}