I'm creating an AVAudioFile for writing sound to a sound file. If the file already exists I want to move the framePosition to the end of the file, to continue writing at the end, instead of replacing the existing file.
I check if the file exists, and if it does I set the framePosition property to the same value as the length property. However, the length property is always 0, even if the file exists.
Does creating a new instance of AVAudioFile delete the existing file at the URL you pass in when you create it? What am I missing here?
var boolFileExists = falseif (NSFileManager.defaultManager().fileExistsAtPath(self.recordAudioURL.path!)) { boolFileExists = true}do { try self.audioFile = AVAudioFile(forWriting: self.recordAudioURL, settings: self.engine.mainMixerNode.outputFormatForBus(0).settings) if (boolFileExists) { self.audioFile.framePosition = self.audioFile.length }} catch let error as NSError { NSLog("Error creating AVAudioFile %@", error.localizedDescription)}I also tried to load the file for reading as well and write it's buffer to the self.audioFile
let audioFile = try AVAudioFile(forReading: self.audioRecordURL)let audioFormat = audioFile.processingFormatlet audioFrameCount = UInt32(audioFile.length)let audioBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)try self.audioFile.readIntoBuffer(audioBuffer)self.audioFile.framePosition = self.audioFile.lengthBut the audioFile.length is 0 here as well. Interestingly, when I try to load another file, not written to while recording in my app, it works. So I'm guessing the problem actually is my sound file. Do I have to "finalize" or "close" the sound file after I have written to it to be able to use it like this? I CAN play it manually, if I click on the actual file in the simulator folders on my computer, by the way.