我想在 iOS 上实时录制音频,分析原始音频数据并保存部分录制的数据。我正在使用以下代码记录数据:https ://gist.github.com/hotpaw2/ba815fc23b5d642705f2b1dedfaf0107
现在,我的数据保存在浮点数组中,我想将其保存到音频文件中。我试着用这段代码来做:
let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory, in: .userDomainMask)
let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100] as [String: Any]
let soundFileUrl = dirPaths[0].appendingPathComponent("recording-" + getDate() + ".pcm")
do {
let audioFile = try AVAudioFile(forWriting: soundFileUrl, settings: recordSettings)
let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100, channels: 2, interleaved: true)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 3000)
for i in 0..<circBuffer.count {
audioFileBuffer.int16ChannelData?.pointee[i] = Int16(circBuffer[i])
}
try audioFile.write(from: audioFileBuffer)
}
在最后一行,我收到一条错误消息:
错误:> avae> AVAudioFile.mm:306:-[AVAudioFile writeFromBuffer:error:]:错误-50幅值Demo(79264,0x70000f7cb000)malloc:*对象0x7fc5b9057e00的错误:已释放对象的校验和不正确-对象在被释放后可能被修改. *在 malloc_error_break 中设置断点进行调试
我已经搜索了很多其他问题,但找不到任何对我有帮助的东西。