如何修复:“49:CreateDataFile 失败”

How to fix: "49: CreateDataFile failed"

我正在尝试按照教程在 swift 中编写一个简单的录音机,但由于某些原因录音机在 运行 .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 失败”错误 none

"applicationDirectory" 造成了问题。将其更改为 "documentDirectory" 解决了它。