我正在尝试按照教程快速编写一个简单的录音机,但是由于某种原因,录音机在运行.record()时失败,并引发以下错误
我尝试过不同的文件记录设置和更改文件位置的设置,没运气。
let audioFilename = getFileURL(tag: tag)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
] as [String : Any]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()}
还有getFileURL方法:
func getFileURL(tag: Int) -> URL {
let path = getDocumentsDirectory().appendingPathComponent("\(HelperFunctions.getRecordingName(tag: tag)).m4a")
return path as URL
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask)
return paths[0]
}
我收到“ 49:CreateDataFile失败”错误,应该为空
答案 0 :(得分:0)
“ applicationDirectory”创建了问题。将其更改为“ documentDirectory”即可解决。